hidden_field syntax problems - ruby-on-rails

I have been trying to pass what round the users intends to go to by which button they click through a hidden field that passes in the round number.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
With this code I constantly get 2 passed through as the round in my pick_page. It is obviously skipping over the first hidden field. How can I get it so 'f.submit' submits the round number that is associated with its class.

We just had the same problem! We fixed it creating 2 diferent forms right next to each other.
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>
Change it to
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '1', :class =>'round1' %>
<%= f.submit 'Picks', :class => 'round1' %>
<% end %>
<%= form_for #round, :url => { :action => 'pick_page'} do |f| %>
<%= f.hidden_field :round, :value => '2', :class =>'round2' %>
<%= f.submit 'Picks', :class => 'round2' %>
<% end %>

You can just remove the hidden fields and add a name and value attributes on your submit button.

Related

Rails 4 Select Not Displaying Selected

This is a short one. I have a select box that is in a form used to create and edit a group. It works, saves the selected option to the database, but the form does not display what is in the database, it always shows the first option.
<%= form_for #group, :html => {:multipart => true} do |f| %>
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], #group.privacy), {}, {:class => 'form-control'} %>
<%= f.submit :class => 'btn btn-primary' %>
<% end %>
I have also tried:
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], :selected => #group.privacy), {}, {:class => 'form-control'} %>
Again, that save the data but does not display the selected option.
Thanks for any help you can offer.
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], :selected => f.object.privacy), {}, {:class => 'form-control'} %>
Try this
Use this code:
<%= form_for #group, :html => {:multipart => true} do |f| %>
<%= f.select :privacy, options_for_select([["Public", "Public"], ["Private", "Private"]], :selected => f.object.privacy), {}, {:class => 'form-control'} %>
<%= f.submit :class => 'btn btn-primary' %>

Field named value in Rails

I'm using 'rails-settings-cached' gem and I want to add ability of creating/updating settings in backend. Model has fields: 'var', 'value', etc... As form builder I'm using Formtastic-bootstrap.
When I doing f.input :value I get no implicit conversion of nil into String. I think that the reason is that 'value' is a keyword.
How can I solve my problem?
Thank you!
UPD:
<%= semantic_form_for [:admin, #setting], :html => {:class => "form-horizontal"} do |f| %>
<%= f.semantic_errors :var, :value%>
<%= f.inputs do %>
<%= f.input :var %>
<%= f.input :value, :as => :text %>
<% end %>
<%= f.actions :class => 'form-group' do %>
<div class="col-lg-offset-2 col-lg-8">
<%= f.action :submit %>
<%= f.action :cancel %>
</div>
<% end %>

Rails slim form_for tag

I am having trouble getting a form to work after switching from regular ERB to slim files. Here is the form I am trying to have slim render:
= form_for #student, :url => students_path(#student), method: :post do |f|
= f.hidden_field :student_id, :value => current_user.id
= f.hidden_field :course_id, :value => group.id
= submit_tag "Join this Class!", :class => "btn btn-primary pull-right join-button"
Here was the working code in regular ERB file
<%= form_for #student, :url => students_path(#student), method: :post do |f| %>
<%= f.hidden_field :student_id, :value => current_user.id %>
<%= f.hidden_field :course_id, :value => group.id %>
<%= submit_tag "+ Join", :class => "btn btn-primary pull-right join-button" %>
<% end %>
This is the error I am currently getting:
undefined local variable or method `f'
Most problem with slim it is indents try this(two space after form_for):
= form_for #student, :url => students_path(#student), method: :post do |f|
= f.hidden_field :student_id, :value => current_user.id
= f.hidden_field :course_id, :value => group.id
= submit_tag "Join this Class!", :class => "btn btn-primary pull-right join-button"

Making a form_tag specific for each user when running each do

I'm trying to create a feedback form where each student can list 2 interests.
However, with my code below, it only recognizes interest_1 and interest_2 for the last student and maps the values to all students, instead of a different value for each student. Anyone can shed some light on how to make interest_1 and interest_2 specific for each student?
<%= form_tag feedback_send_path, :method => 'post' do %>
<% #course.students.each do |student| %>
<%= avatar_for(student, :photo_size => :thumb_small, :class => 'notBig', :size => '50x50') %>
<%= link_to student.name, user_path(student.id), :class => "profile-link" %>
<%= text_area_tag 'interest_1', '', :class => 'feedback-form', :placeholder => "Wants to teach..." %>
<%= text_area_tag 'interest_2', '', :class => 'feedback-form', :placeholder => "Wants to learn..." %>
<% end %>
<%= submit_tag "Submit", :class => "send-message-button" %>
<% end %>
`
Instead of
<%= text_area_tag 'interest_1', '', :class => 'feedback-form', :placeholder => "Wants to teach..." %>
<%= text_area_tag 'interest_2', '', :class => 'feedback-form', :placeholder => "Wants to learn..." %>
Use
<%= text_area_tag 'interest_1[]', '', :class => 'feedback-form', :placeholder => "Wants to teach..." %>
<%= text_area_tag 'interest_2[]', '', :class => 'feedback-form', :placeholder => "Wants to learn..." %>
Then have a look at your params Hash!

Ruby on Rails - How to pass hash parameter to a partial view?

'shared/subscription' %>
To call this partial view:
<% form_for(:subscription, :url => city_subscriptions_path(#city)) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
Since I am using this partial view on different places, how do I alter the caller so it will pass a hash for the form_for helper? So it would be like this when the helper is called:
<% form_for(:subscription, :url => city_subscriptions_path(#city), :html => {:id => 'main_input' }) do |form| %>
<%= form.hidden_field :city_id, :value => #city.id %>
<%= form.text_field :email, :size => 30 %>
<%= form.submit "Email Me" %>
<% end %>
<%= render :partial => "shared/subscription", :locals => {:foo => "bar", :foofoo => ["bar", "bar"]}
In your partial view, use them:
<%= foo #this outputs "bar" %>
<%= foofoo.to_s %>

Resources