Rails form fields empty after submitting form - ruby-on-rails

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 .

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.

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 %>

Related

accessing related record attributes in nested form

In a nested form, that is only used for updates
<%= form_for(#event) do |f| %>
<%= f.fields_for :distances do |distance| %>
<%= distance.check_box :active %>
the form functions in updating the child record. However I cannot call any attirbute of the related record with either for distanceprefix
<%= distance.starting_point %>
How can this value be rendered?
Try like this, it create new object only if not present:
<%= form_for(#event) do |f| %>
<%= f.fields_for :distances, f.object.distances || f.object.build_distances do |distance| %>
<%= distance.check_box :active %>
<%= distance.starting_point %>
<%= distance.object.starting_point %>
renders the child attribute

add a tag by another user by using acts-as-taggable-on

By using acts-as-taggable-on, I made possible to add tags to a word model. And each word belongs to a user.
And I want to make it possible another user can add a tag but for a given amount of time after adding a tag the user can't add another tag.
To implement the function I'm stuck at a error params not found: word.
I rewrite a form
<%= form_for [word.user, word] do |f| %>
<%= f.text_field :tag_list %>
<%= f.submit 'Edit' %>
<% end %>
to
<%= form_tag add_tag_user_word_path(word.user, word) do %>
<%= text_field_tag :tag %>
<%= submit_tag 'Add' %>
<% end %>
then I got the params not found error, when I submit the form.
I could not find the way to pass params[:word] by using form_tag. How can I do it?
I found that I can write like "word[tag]" like this.
<%= form_tag add_tag_user_word_path(word.user, word) do %>
<%= text_field_tag "word[tag]" %>
<%= submit_tag 'Add' %>
<% end %>
Then it works as I hope.

How to submit multiple, duplicate forms from same page in Rails - preferably with one button

In my new views page I have:
<% 10.times do %>
<%= render 'group_member_form' %>
<% end %>
Now this form contains the fields: first_name, last_name, email_address and mobile_number. Basically I want to be able to fill in the fields of all the forms in one click which then submits each into the database as a unique row/id.
What would be the easiest way to accomplish this?
Note: The number of times do is called from a variable. Any advice welcome, thanks!
You should have only one form (you should put only fields in the group_member_form partial). In your view you should have something like:
<%= form_tag "/members" do %>
<% 10.times do %>
<%= render 'group_member_form' %>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
and in _group_member_form.html.erb you should have
<%= text_field_tag "members[][first_name]" %>
<%= text_field_tag "members[][last_name]" %>
<%= text_field_tag "members[][email_address]" %>
<%= text_field_tag "members[][mobile_number]" %>
This way, when the form submits, params[:members] in the controller will be an array of member hashes. So, for example, to get the email adress from the fourth member after submitting the form, you call params[:members][3][:email_adress].
To understand why I wrote _group_member_form.html.erb like this, take a glance at this:
http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions.
You can also use accepts_nested_attributes_for in your model, and use fields_for on your form.
Submitting multiple forms, afaik, only javascript, if the forms are remote: true, and you run through each of them and then submit.
$("form.class_of_forms").each(function() {
$(this).submit();
});
Alternatively a more up to date approach using form_with and fields_for, without removing the form into a partial, could be written like this:
<%= form_with (url: end_point_path), remote: true do |form| %>
<% (1..5).each do |i| %>
<%= fields_for 'cart_items'+[i].to_s do |fields|%>
<%= fields.text_field :first_name %>
<%= fields.text_field :last_name %>
<%= fields.email_field :email_address %>
<%= fields.number_field :phone_number %>
<% end %>
<% end %>
<%= form.submit "Submit" %>
<% end %>

How do I send information with a form_for form?

I have two submit buttons both using the same create action in my controller. How can I send information with a form (without the user inputting it)?
Example:
Button 1: Creates a database entry with 1.
Button 2: Creates a database entry with 2.
You can use a hidden field like this:
<%= form_for #model do |f| %>
<%= f.hidden_field :entry, 1 %>
<%= f.submit 'Button 1' %>
<%= form_for #model do |f| %>
<%= f.hidden_field :entry, 2 %>
<%= f.submit 'Button 2' %>

Hiding Checkbox & Assigning Value - Ruby on Rails - Easy Question

I am trying to hide a checkbox and assign a default value of 1 such that the submit button only shows. Here is my form. Just wondering as the proper format as I am new to rails. I think you can do this with helpers but was wondering if I can just include it in the form. Here is the form:
<% remote_form_for [#post, Vote.new] do |f| %>
<p>
<%= f.label :vote %>
<%= f.check_box :vote %>
</p>
<%= f.submit "Vote" %>
You can certainly do this, but if all you want is to set a parameter without displaying a field, what you probably want instead is a hidden field:
<%= f.hidden_field :vote, :value => '1' %>
If you really do want a hidden checkbox (maybe so you can optionally display it later using javascript?), you can do it like this:
<%= f.check_box :vote, :checked => true, :style => 'visibility: hidden' %>
You could use CSS to hide the checkbox:
<%= f.check_box_tag :vote, 1, true, :style => "display: none;" %>
But if you just want to pass a value you can just use a hidden field:
<%= f.hidden_field_tag, :vote, 1 %>
If you just want to pass the value along, use a hidden field
<% remote_form_for [#post, Vote.new] do |f| %>
<%= f.hidden_field_tag 'vote', '1' %>
<%= f.submit "Vote" %>
<% end %>

Resources