(rails) hidden form elements not giving values to controller - ruby-on-rails

I have the following form:
<% form_for(:tag, :url => {:action => "post_tag", :id => #photoID}) do |form| %>
<%= error_messages_for(:tag) %>
<% if #errors then %>
<%= #errors[0] %>
<% end %>
<p><%= form.select(:user_id, #userHash) %></p>
<p><%= form.hidden_field(:xpos) %></p>
<p><%= form.hidden_field(:ypos) %></p>
<p><%= form.hidden_field(:width) %></p>
<p><%= form.hidden_field(:height) %></p>
<%= submit_tag "Submit Tag" %>
<% end %>
But none of the values are filled in the controller. I know the values are all full in the view because I can see they have the correct values in Firebug. In the controller, I am trying to access them like params[:xpos] for the :xpos hidden_field. Is this correct???

Do logger.debug params.inspect. I have a sneaking suspicion you will see params[:tag][:xpos] there :)
You have passed a name to form tag there (form_for(:tag, ...) do |form|), it will wrap all fields constructed as form.field(...) in a hash identified by the passed name ("tag", in this case).

Related

Persist radio_button value on form_tag submit get method

I think this is quite simple, but really struggling here:
<%= form_tag(admin_articles_path, method: "get") do %>
<p><%= radio_button_tag(:filter, "all") %>
<%= label_tag(:filter, "All") %></p>
<p><%= radio_button_tag(:filter, "pub") %>
<%= label_tag(:filter, "Published") %></p>
<p><%= radio_button_tag(:filter, "unpub") %>
<%= label_tag(:filter, "Unpublished") %></p>
<p><%= radio_button_tag(:filter, "feat") %>
<%= label_tag(:filter, "Featured") %></p>
<p> <%= submit_tag("Show", class: "btn btn-sm btn-primary") %></p>
<% end %>
I have some sorting logic in my controller, and when I submit this I want the radio_button value to persist to the new view. How do I do this?
Thank!
You want to do this sort of thing. For example, with the all option...
<p><%= radio_button_tag(:filter, "all", params[:filter] == "all") %>
The 3rd param is a boolean that says whether or not it is checked.
You will also need something to uncheck a radio. I use this...
<p><%= radio_button_tag(:filter, '', params[:filter].nil?) %>
In your receiving controller action:
#filter = params[:filter]
You then have access to the radio button selection in your respective view.

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

create function in controller not saving two separate params

these are the codes
<p>
<% form_for #movie do |m| %>
<%= m.label :title, 'Title' %>:
<%= m.text_field :title %></br>
<%= m.label :release_year, 'Release Year' %>:
<%= m.select :release_year, (1900..2011) %></br>
</br>
<% Movie.genres.each do |genre| %>
<%=h genre %>
<%= check_box_tag :genre, genre %></br>
<% end%>
</br>
<%= m.submit "Save" %>
<% end %>
</p>
and my code in the controller:
def create
#movie = Movie.new(params[:movie].merge(:genre))
if #movie.save!
render show_path
else
render new_path
end
But for whatever reason, I keep getting error messages saying "undefined method `each_pair' for :genre:Symbol" or "cannot find Movie without an ID". It's not saving properly.
Is it because of my submit form is only the movie form |m| or is it because my create function in the controller is wrong?
Thanks.
perhaps instead of
params[:movie].merge(:genre)
you wanted to do
params[:movie].merge(:genre => params[:genre])
?
Check Firebug in Firefox (or the Chrome developer tools if you're using Chrome, or your app logs if you're using neither), and see what parameters are actually being passed in your scenario? Sounds like your code is expecting to receive a parameter that is not actually being passed. The tools/log should reveal where the breakdown is happening.

How do you pass additional information into a nested form partial?

Based on Railscast 196 and 197, I've created a nested form for adding multiple illustrations (image attachments) to my article model. I have a form like this:
<%= form_for #article, :html => { :multipart => true } do |f| %>
<h2>Illustrations</h2>
<% for illustration in #article.illustrations %>
<%= f.fields_for :illustrations, illustration do |builder| %>
<%= render :partial => "illustration_fields",
:locals => { :f => builder, :illustration => illustration } %>
<% end %>
<% end %>
<p>
<%= f.submit "Submit" %>
</p>
<% end %>
And the _illustration_fields partial looks like this:
<div class="fields">
<p>
<%= f.label :illustration, "Illustration" %><br />
<%= link_to_remove_fields "remove", f %><br />
<%= f.file_field :illustration %>
</p>
</div>
If the illustration already exists, I want to display it on the page inside an image tag. I tried displaying the image inside the partial like this:
<% unless :illustration.illustration.blank? %>
<%= image_tag(:illustration.illustration.url) %>
<% end %>
But it throws up an error:
undefined method `illustration' for :illustration:Symbol
The illustration field definitely exists in the illustration model, as I can display the image inside the main form like this:
<% unless illustration.illustration.blank? %>
<%= image_tag(illustration.illustration.url) %>
<% end %>
It seems that Rails doesn't understand the illustration information into the partial (or I'm not passing it correctly). What is the correct approach to going about this?
Solved it. The illustration object can be accessed from inside the partial as follows. No need to pass it inside the :locals hash.
<% unless f.object.illustration.blank? %>
<%= image_tag(f.object.illustration.url) %>
<% end %>
You got the symbol error because you are accessing the local using : in the unless.
The error
undefined method `illustration' for :illustration:Symbol
means that Ruby looked for a method named illustration on an instance of the Symbol class; the method obviously is not meant to exist in the Symbol class.
The way to access the local variable that is passed in is simply illustration.whatever.whatever (no : is used!).

Resources