send data to an array from 2 collection select boxes - ruby-on-rails-3

I have controller with an action step2 which collects all devices by selected category. My step2.html.erb looks like:
<% form_for compare_comparision_path, :url => {:action => 'comparision'} do |f| %>
<%= f.collection_select(:device, #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
</br>
<%= f.collection_select(:device, #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
<%= f.submit 'ok' %>
<% end %>
I want for it to allow the user to select two devices and send it to some array or variable in comparison action.

You can do so:
<% form_for compare_comparision_path, :url => {:action => 'comparision'} do |f| %>
<%= f.collection_select('device[]', #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
</br>
<%= f.collection_select('device[]', #devices, :id, :name, options ={:prompt => "Select"}, :class=>'device') %>
<%= f.submit 'ok' %>
<% end %>
and in controller you`ll have an array in params[:device] containing to selected values.
Or you can replace 'device[]' in my example with unique name for each selectbox (for example 'devise1' and 'device2'.
Then you can get selected values in controller by accessing params[:device1] and params[:device2]

Related

How to disable certain check_boxes when using Formtastic's :as => :check_boxes

<%= semantic_form_for [:admin, #admin] do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :email, :as => :email %>
<%= f.input :password, :as => :password %>
<%= f.input :password_confirmation, :as => :password %>
<%= f.input :admin_roles, :as => :check_boxes, :required => true, :disabled => [1] %>
<% end %>
<%= f.actions %>
<% end %>
-
<%= f.input :admin_roles, :as => :check_boxes, :required => true, :disabled => [1] %>
Passing an array to the above code disables the checkbox with id=1, which is nice. But I want to check the model of each checkbox, too see if it has a specific value, so I can disable it or not.
do_not_disable_checkbox if admin_role.do_not_show_me_boolean_field
Do I need to iterate each of my :admin_roles and output a checkbox for each? Or can I do it in one line like above? I'm new to rails and ruby, and I can't wrap my head around it and hoping for some help to put me in the right direction.

Creating nested form in Rails 3.1

I am trying to render a partial which I have set up as the following. I have am also trying to create a nested form whereby I have included accepts_nested_attributes_for :user in my hospital_bookings model. I seem to be getting the following error:
NameError in Rota_days#index
Showing
C:/Users/home/Desktop/Portal/app/views/rota_days/index.html.erb
where line #31 raised:
undefined local variable or method `hospital_booking' for
which is pointing to the following line <%= render :partial => "booking_dialog", :locals => { :booking => hospital_booking.new } %> of my index.html.erb as shown below. I thought it was something to do with my pluralization. By changing hospital_bookings.new to hospital_booking.new but this did not work
_booking_dialog.html.erb
<%= form_for booking do |f| %>
<%= f.fields_for :user do |f| %>
<br/>
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
hospital_booking.new is nonsensical: you have no local variable named hospital_booking. If you want a new instance of the HospitalBooking model, then you want HospitalBooking.new.
So:
<%= render :partial => "booking_dialog", :locals => { :booking => HospitalBooking.new } %>
Update (from the comments)
In the booking_dialog form partial, you need to put the name attribute on the associated user record inside a fields_for block, to distinguish it from the fields for the parent (booking):
<%= form_for booking do |f| %>
<%= fields_for :user do |user_fields| %>
<%= user_fields.label :name %>
<%= user_fields.text_field :name %>
<% end %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
p.s. it seems very strange that you have a hidden field for the :id in here. You shouldn't need that.

Simple Form: Non object related attribute. How to?

I have a simple form that looks like this:
<%= simple_form_for #study,:url => studies_path, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :category, :collection => #categories, :label => "Category", :selected => #categories.first %>
<%= f.button :submit, t('add_form'),:class => 'btn-primary' %>
I want to add a new field in that form that will be merely visual sugar. It will be a helper to dynamically change the :category field.
How do I do that?
Use text_field_tag or whichever other _tag helper to generate a form element without attaching it to any specific attribute of the model:
<%= simple_form_for #study,:url => studies_path, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :category, :collection => #categories, :label => "Category", :selected => #categories.first %>
<%= text_field_tag :blah %>
<%= f.button :submit, t('add_form'),:class => 'btn-primary' %>
<% end %>

Couldn't find <model> without an ID, with nested attributes

I have what should be a super simple form which I'm trying to use to update multiple records at once. I'm on Rails 3. I've been through all of the railscasts, etc and am pulling my hair out at this point. I'm using Devise, and have a contacts controller. Users have_many :contacts, and accepts_nested_attributes_for :contacts. The form looks like:
<%= form_for #user, :url => '/updateusercontacts' do |i| %>
<%= i.fields_for :contacts do |f| %>
<%= f.label :first_name %>
<%= f.text_field :firstname %>
<%= f.label :last_name %>
<%= f.text_field :lastname %>
<%= f.label :phone_number %>
<%= f.text_field :phonenumber %>
<%= f.label :user_id %>
<%= f.text_field :id %>
<p>
<% end %>
The form displays properly, but then on submit I get "Can't find Contact without an ID". The controller looks like:
def updatecontacts
#contacts = Contact.find(params[:id])
#contacts.each do |contact|
contact.update_attributes(params[:id])
end
render '/home'
end
The parameters seem correct, and the id's seem to be present and correct, but I can't get the save to work! I'm sure I'm missing something obvious here.
Try to remove the fields_for and when you invoke your find, use this [:user][:contacts]
your code should look like this.
View:
<%= form_for #user, :url => '/updateusercontacts' do |i| %>
<%= i.label :first_name %>
<%= i.text_field :firstname %>
<%= i.label :last_name %>
<%= i.text_field :lastname %>
<%= i.label :phone_number %>
<%= i.text_field :phonenumber %>
<%= i.label :user_id %>
<%= i.text_field :id %>
<p>
<% end %>
Controller
def updatecontacts
#contacts = Contact.find(params[:user][:contacts])
#contacts.each do |contact|
contact.update_attributes(params[:user][:contacts])
end
render '/home'
end
Hope this works.

Rails is not creating hidden field for put method on an update form

I have a REST resource called Greetings.
Here is my routes file:
resources :greetings
I have a form to create or update a greeting as follows:
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form') do %>
<%= text_area :greeting, :content, :rows => 3, :placeholder => "Type your message..." %>
<% end %>
note: I am using form_tag because this form also collects user data.
This is supposed to create a hidden field with method=> put but its not so it can't find the route.
Any ideas how I can get this to submit to the update action?
Just write
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form', :method => :put) do %>
and everything should be working.
You can use form_for tag and still collect user data like this:
<%= form_for #greeting, :validate => true do |f| %>
<%= f.text_area :content, :rows => 3, :placeholder => "Type your message..." %>
<%= f.fields_for #user do |u| %>
<%= u.text_field :name %>
<% end %>
<% end