Managing Roles with Checkboxes with Rolify - ruby-on-rails

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?

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

hidden_field syntax problems

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.

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!

how to bind custom action to submit button on a form?

I made a form, but I don't know how to collect data from it and after pressing submit button redirect user to adding/fill.erb file, here is my form:
file: adding/counter.erb
<%= simple_form_for :counter do |f| %>
<%= f.input :first_name, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :last_name, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :city, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :postal, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :street, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :job, :collection => 0..10 , :prompt => "How many?" %>
<%= f.input :role, :collection => 0..10 , :prompt => "How many?" %>
<%= f.button :submit, 'next step', :style => "margin-top: 20px;" %>
<% end %>
I know it is a lame question, but I'm working on it for several hours and I don't know what to do:/
I'm not entirely clear, but it sounds like your trying to set the URL the form is posted to? If so, you should be able to do this:
<%= simple_form_for :counter, :url => {:controller => "counter", :action => "fill} do |f| %>

Flash[:notice] remains after form submission

When the form gets submitted the flash message is displayed and the user remains at the same page with all fields filled.Now if one of the required fields id deleted and again we submit the form, the error is not displayed and the flash[:notice] is displayed.When i refresh the page the notice goes off.unable to figure out the reason .i have given the view code and in controller
flash[:notice] = "Saved Successfully"
View code:
<b><h3><%= flash[:notice] %></h3></b>
<% semantic_form_for(#featured_business, {:url => "#{#signin_link}".gsub(/\/+/, '/'), :html => {:multipart => true, :class => 'validate business'}}) do |f| %>
<% f.inputs do %>
<%= hidden_field_tag 'more_validations_required' %>
<%= f.input :name, :label => ' Name:' , :input_html => { :style => "width:240px;" }%>
<%= f.input :contact_name, :label => 'Contact name:',:required => false, :input_html => { :style => "width:240px;" } %>
<%= f.input :phone, :label => 'Phone Number:', :input_html => { :style => "width:240px;" } %>
<%= f.input :email, :label => 'Email:', :input_html => { :class => 'email' } , :input_html => { :style => "width:240px;" } %>
<%= f.commit_button :label => "", :button_html => {:class => 'signup business'} %>
<% end %>
<% end %>
Flash messages exist for the next page only. If you refresh the page then the flash message is no longer stored in the session.
Here is a good blog post to help you understand flash messages. http://travisonrails.com/2008/08/17/working-with-the-flash-hash

Resources