On Sun, Aug 1, 2010 at 06:52, Michael Pavling <[email protected]> wrote:
> On 1 August 2010 11:32, Dis Tec <[email protected]> wrote: >> I'm returning a value from the db which is true/false and I'd like to >> convert that to something more meaningful - how can I incorporate an if >> then into the inline html? > > You can use conditional checking inline, with "normal" 'if...else' blocks: > <% if @my_object.active? %> > This is active > <% else %> > You need to activate this one > <% else %> > > 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. 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. (BTW, note the single quotes; I haven't verified it myself, but heard that they are at least marginally faster for constant strings (i.e., where you *can* use them), since the system won't even *try* to look for vars that need to be interpolated. Makes sense to me.) Now back to the Original Poster. Dis Tec had also written: >> I have tried if ... puts "xxx", but to no avail. That will make it put xxx on the Ruby console, not in the output to be sent back to the HTTP client. 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. 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. I'm not going to go into depth on them here myself, as there are gazillions of web pages that explain them well. Rails is *highly* "opinionated" and makes a lot of assumptions. Learning those opinions and assumptions (such as, that you're using the MVC approach), how to use them, and how to override them when absolutely necessary, will help you immensely. If you find a good tutorial site or book on Rails, it should go into depth on both of these, and a lot of other fundamentals you'll need. > 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, tho IMHO the OP didn't do too bad a job. Fairly clear phrasing, he told us what he tried, he didn't cop a 'tude, and the main thing he needs to know isn't all THAT obvious, at least if he hasn't gone thru a good complete RoR tutorial. -Dave -- Specialization is for insects. -RAH | Have Pun, Will Babble! -me Programming Blog: http://codosaur.us | Work: http://davearonson.com Leadership Blog: http://dare2xl.com | Play: http://davearonson.net * * * * * WATCH THIS SPACE * * * * * | Ruby: http://mars.groupsite.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.

