Reputation: 5320
I'd like to output a form button:
<% content_tag :button :type => :submit, :class => :positive do %>
<%= image_tag "icons/tick.png"%>
Save
<% end %>
Which should generate:
<button type="submit" class="positive">
<img src="/images/icons/tick.png" alt=""/>
Save
</button>
I have this throughout my site, it's getting messy and I just learned that Rails has helper methods. I was curious. Would it be possible to build a helper method so I could just say something like this in rails
<%= form_button(submit) %>
What do you think? Would this live in the application_helper.rb file?
Something like:
def form_button (type)
if type == 'submit'
<% content_tag :button :type => :submit, :class => :positive do %>
<%= image_tag "icons/tick.png"%>
Save
<% end %>
end
end
Upvotes: 0
Views: 805
Reputation: 9456
To answer your first question: Yes it is possible and that is what the Rails framework encourages: reuse of code (otherwise known as the DRY-principle). Specific to view logic, there is a helper for each and every one of your models. This is the default/convention, though it is not something you have to adhere to. I'll explain more about this in the next paragraph. So yes, put your form_button
method into a helper -- not necessarily application_helper.rb
.
To answer your second question: You could stick it in application_helper.rb
, but there's nothing stopping from you making things a little more logical (i.e., creating a buttons_helper.rb
). In Rails 3 specifically, all helpers are available to each and every view (though this has ruffled some feathers). So what you could do instead is create a new helper for yourself (i.e., rails g helper Buttons
), specifically for creating your buttons, and put your logic in there.
Take a look at this Railscast. It describes in more detail exactly what I've said above. I had the same question and found it very helpful. http://railscasts.com/episodes/101-refactoring-out-helper-object
Upvotes: 2
Reputation: 42865
If the helper is going to be all around the site then application is probably the best place to put it for helpers.
You could do what you have, you don't need to put the <%%>
tags since those are for erb
Embeded Ruby which are your view extensions and helpers are just .rb
so there is not need for that.
I checked the API and it looks like you don't need to role your own helper for this:
content_tag(:p, "Hello world!")
# => <p>Hello world!</p>
content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
# => <div class="strong"><p>Hello world!</p></div>
content_tag("select", options, :multiple => true)
# => <select multiple="multiple">...options...</select>
A better way to do this is apply the image as a background image in you stylesheet or do it inline. You don't need to make a helper for this and BTW what happens if the type is not 'submit'? Do you have other inputs to check for or a default? If not it doesn't make sense to wrap it in a helper.
so you can do this"
<% submit_tag "Save", :class => "your_class", :style => "font-size:10px;"%>
or
<% f.submit "Save", :class => "your_class", :style => "font-size:10px;"%>
Upvotes: 0