Form tags in a ruby view need to be outside of any table elements or the form/submit button will not work.

Last Updated on September 15, 2020 by Christopher G Mendla

If you are using form tags in a ruby view, they need to be outside of any table elements or the form/submit button will not work properly.

The following will work:

<%= form_tag documents_path, :method => 'get' do %>
  <table>
    <tr>
      <td>
         <%= text_field_tag :search, params[:search] %>
      </td>
      <td>
         <%= submit_tag "Search", :name => nil %>
      </td>
    </tr>
  </table>
<% end %>       


The following will NOT work:

<table>
  <tr>
    <td>
      <%= form_tag documents_path, :method => 'get' do %>
      <%= text_field_tag :search, params[:search] %>
    </td>
    <td>
      <%= submit_tag "Search", :name => nil %>
      <% end %>
    </td>
 </tr>
 </table>

I’m not sure why this is the case but I’ve run into it a number of times.

Ruby 2.x/Rails 4.x

Leave a Reply

Your email address will not be published. Required fields are marked *