2

This is my form:

<%= form_tag("/adverts", :method => "get") do %>
Order by: 
<%= select_tag :order_by, options_for_select([['Ascending', 'ASC'], ['Descending', 'DESC']])%>

<%= text_field_tag :text%>

<%= submit_tag 'Change' %>
<% end %>

In my Adverts controller, index method, for now I am not doing anything and I can see that it is getting correct values from form,

=>but when page reloads after submission, fields values are empty but I want them to retain values.

So if I enter some text in text field, that text will still be there after submitting form .

1
  • 1
    Did you look at the API for options_for_select and text_field_tag? That's where you can find the answer to your question. Commented May 23, 2013 at 10:50

2 Answers 2

5

Like this:

<%= select_tag :order_by, options_for_select([['Ascending', 'ASC'], ['Descending', 'DESC']], params[:order_by]) %>

and:

<%= text_field_tag :text, params[:text] %>

See the API for options_for_select and text_field_tag.

Sign up to request clarification or add additional context in comments.

1 Comment

A more appropriate answer than mine.
1

You need to create the form for an object if you want it to automatically get the objects values on reload.

<%= form_for @object do |form| %>
  <%= form.text_field :name %> <!-- automatically gets re-populated with the value of @object on postback -->
  <%= form.submit %>
<% end %>

If you really want to use form tags instead of a builder then you need to set the values manually after postback

<%= text_field_tag :text, some_string_value %>

1 Comment

@Mischa I only said it needs an object if OP wants it to re-populate automatically, which is true. Using form tags requires manually re-populating the values, as I also explain in the answer. Your answer is more direct so I'll upvote that one, but leave my answer for additional context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.