Ryan-Neal Mes
Ryan-Neal Mes

Reputation: 6263

Rails - can't figure out how to write form

I would like to have search text boxes as the headers of a bunch of table columns. How would I write this?

So far I have managed to get it working without the text_fields in the headers of the table.

<%= form_tag contacts_path, :method => 'get' do %>
  <%= text_field_tag :email_address, params[:email_address]  %>
  <%= text_field_tag :first_name, params[:first_name]  %>
  <%= text_field_tag :last_name, params[:last_name]  %>
  <%= submit_tag "Search", :name => nil %>
<% end %>

<table>
  <thead>
    <tr>
      <th><%= text_field_tag :email_address, nil, placeholder: "email address ..."  %></th>
      <th><%= text_field_tag :first_name, nil, placeholder: "first name ..."  %></th>
      <th><%= text_field_tag :last_name, nil, placeholder: "last name ..."  %></th>
    </tr>
  </thead>
  <% end %>
  <tbody>
    <% @contacts.each do |contact| %>
    <tr>
      <td><%= link_to contact.email_address.to_s, edit_contact_path(contact) %></td>
      <td><%= contact.first_name %></td>
      <td><%= contact.last_name %></td>
    </tr>
    <% end %>
  </tbody>
</table>

Right now I have my functionality working, but it isn't the way I want. I want each text field in the headers to be doing what the text fields in the top form do. Is there an easy way to do this?

Thanks.

Upvotes: 0

Views: 42

Answers (1)

Jon
Jon

Reputation: 10898

Your text fields are not enclosed within your form. They must exist somewhere in the middle of this:

<%= form_tag contacts_path, :method => 'get' do %>
  # ... All form elements must be within your form ... in here!
<% end %>

If they exist outside of the form they will not be submitted with the form to your application.

Just move your table inside your form and put the fields where you need them, and everything should work.

Upvotes: 1

Related Questions