Is there a way with rails form helper to produce a button tag for submit

Jonathan picture Jonathan · Feb 12, 2010 · Viewed 25.9k times · Source

I am trying to create buttons ala Wufoo (Rediscovering the button element)

I would like to write the following code like the following:

<%form_tag search_path, :method => :get, :class => 'search' do %>
  <%=text_field_tag :search, params[:search] -%>
  <%=button_tag 'search', :name => nil-%>
<%end%>

To generate the following HTML (instead of the input[type="submit"] tag)

<button type="submit" class="positive">
    <img src="/images/icons/tick.png" alt=""/> 
    Save
</button>

Does a method exist already? Or should I roll my own helper?

Answer

EmFi picture EmFi · Feb 12, 2010

You could use content_tag to achieve this. It's the more railsy way of doing what you want. But it's longer than the raw HTML.

<% content_tag :button :type => :submit, :class => :positive do %>
   <%= image_tag "icons/tick.png"%>
   Save
<% end %>

Which produces

<button type="submit" class="positive">
    <img src="/images/icons/tick.png" alt="Tick"/> 
    Save
</button>

However using this as a starting point you should have no problem rolling your own robust helper, or abstracting it to a partial.