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' %>
Related
This is my select tag in ruby, after submitting the form ,i want to edit the selected option. so that i done <%= render 'form' %> in my edit.htm.erb ,but the option that i submitted is not appearing in the select field, when i click edit button. it shows select a category
Please suggest me a solution to solve this
form.html.erb
<%= simple_form_for([:coaches, #programme]) do |f| %>
<%= f.input :title %>
<%= select_tag 'category', options_from_collection_for_select(#categories, 'id', 'name',#categories.category_id), :class => "wrapper-dropdown-3_1", :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>
<%= f.button :submit, "PUBLISH", :class => "btn_style" %>
edit.html.erb
<%= render 'form' %>
#categories = Category.all.map{|c| [c.name, c.id]}
<%= simple_form_for([:coaches, #programme]) do |f| %>
<%= f.input :title %>
<%= f.input :category, as: :select, collection: #categories, selected: f.object.category, input_html: { class: 'wrapper-dropdown-3_1'}, :onchange => 'update_subscategories_div(this.value)', include_blank: "Select a Category" %>
<%= f.button :submit, "PUBLISH", :class => "btn_style" %>
<% end %>
You need to pass id of selected element as third argument in options_from_collection_for_select method.
<%= select_tag 'category', options_from_collection_for_select(#categories, 'id', 'name',category_id), :onchange => 'update_subscategories_div(this.value)', prompt: "Select a Category" %>
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"
I currently have a partial that is able to manage roles via radio buttons below:
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<h3>Change Role</h3>
<%= f.input :role_ids, :collection => Role.all, :as => :radio_buttons, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids %>
<%= f.submit "Add or Remove Role", :class => "btn" %>
<% end %>
I want to change this so i can add or remove multiple roles using checkboxes.
<%= simple_form_for user, :url => user_path(user), :html => {:method => :put, :class => 'form-horizontal' } do |f| %>
<h3>Change Role</h3>
<%= f.input :role_ids, :collection => Role.all, :as => :check_boxes, :label_method => lambda {|t| t.name.titleize}, :label => false, :item_wrapper_class => 'inline', checked: user.role_ids %>
<%= f.submit "Add or Remove Role", :class => "btn" %>
<% end %>
But this seems to not work, Here is the console response when using radio buttons:
{"utf8"=>"✓", "authenticity_token"=>"MTV5MNomlkV86ynh0SVR6OW5SG3+9BCznWqcfq2xaWM=", "user"=>{"role_ids"=>"2"}, "commit"=>"Add or Remove Role", "id"=>"4"}
and when using checkboxes:
{"utf8"=>"✓", "authenticity_token"=>"MTV5MNomlkV86ynh0SVR6OW5SG3+9BCznWqcfq2xaWM=", "user"=>{"role_ids"=>["2", "3", "7", ""]}, "commit"=>"Add or Remove Role", "id"=>"2"}
So why isn't it saving to the db? why do i have an empty spot in my array at the end?
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.
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!