Dave, I mostly second your advice here, but there are a couple of things that I disagree with.
Dave Aronson wrote: > On Sun, Aug 1, 2010 at 06:52, Michael Pavling <[email protected]> wrote: > >> On 1 August 2010 11:32, Dis Tec <[email protected]> wrote: > >> >> �or you can use the ternary operator: >> � �<%= @my_object.active? ? "This is active" : "You need to activate >> this one" %> >> >> �or you can use 'if' conditions as guards: >> � �<%= "This is active" if @my_object.active? %> > > Though your approach will work, this looks to me like it's probably > logic that more properly belongs in the controller than the view. Perhaps. Certainly any *more* logic than this in the view is inappropriate, but I think testing the value of a simple method call in the view is OK. > IMHO using something like: > > if @my_object.active? > @activated_msg = 'This is active' > else > @activated_msg = 'You need to activate this one' > end # you did mean end for the 2nd else above, yes? > > or > > @activated_msg = @my_object.active? ? 'This is active' : 'You need to > activate this one' > > or > > @activated_msg = 'This is active' if @my_object.active? > > in the controller, and then using @activated_msg in the view, would be > cleaner. Probably not. I tend to believe that it's a mistake to put display text in the controller in most cases. If it were any more complicated than this, though, I'd set a flag in the model or controller so that a simple boolean value could be tested in the view. [...] > There are two things I think you need to read up on. The first will > answer your general question on how to incorporate Ruby code, > including conditionals and variables, into HTML. The main technique > for doing that, and the default in Rails, is ERB, which works > basically like JSP. (If you're not familiar with JSP, that's OK, just > trying to help the lightbulb turn on.) The main alternate, HAML, has > also gained serious traction, but I suggest you save that for after > you're at least familiar with ERB. I don't know that I'd agree. Haml (not HAML!) works basically like HTML (and therefore ERb [not ERB!]) with less typing. I see no particular reason to use ERb when Haml is available. > > Before you do *any* real work with either, though, I strongly suggest > you read up on MVC, the Model-View-Controller approach. It's one of > the ways to maintain "separation of concerns", a general software > engineering principle. You'll find it helps you organize your code > much more cleanly and maintainably. Rails uses MVC strongly. Agreed. The problem is that every MVC framework has a different interpretation of what MVC is. (I came to Rails from Fusebox, which has an optional MVC design pattern [which I used] that works very differently from Rails' MVC. Cocoa MVC is different again, I believe.) [...] >> Try the following resource as a primer; it should help you organise >> your requests for help to be more likely to elucidate responses: >> http://catb.org/esr/faqs/smart-questions.html > > One of my all-time faves, And no longer mine. ESR does explain -- mostly -- how to ask smart questions, but spends IMHO far too much time saying "look, we don't care about being nice or helpful. Don't expect civility or help when you ask questions. Just fuck off and hope the gurus throw you a bone." That's not how I want the communities I'm part of to work. Best, -- Marnen Laibow-Koser http://www.marnen.org [email protected] -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

