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.
Related
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.
I'm and usings devise for my authentication and would like to keep the details of my users in a separate model called profile. Profile contains information like first and last name. I want to be able to have a single sign up form that will be able to have all this information entered.
This is my form
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages' %>
<% fields_for :profile do |fa| %>
<%= fa.label :first_name %>
<%= fa.text_field :first_name %>
<%= fa.label :last_name %>
<%= fa.text_field :last_name %>
<% end %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
This in my user.rb model
has_one :profile
accepts_nested_attributes_for :profile
When I submit the from it doesn't save the first and last name to the database
After much searching around, this was the simplest answer to my question. Although I'm not sure this the rails way of doing things
Profile model for Devise users?
I am write:
<%= form_for(current_user, :remote => true) do %>
<p>
<%= label_tag t("language") %>:
<%= select_tag "language", options_for_select([["Русский", "rus"], ["English", "eng"]]) %>
</p>
<p><%= submit_tag t "options.save" %></p>
<% end %>
Inspector:
http://deeflow.com/changer/inspect.png
Content:
http://deeflow.com/changer/content.png
But, value in db doesn't updated
<%= form_for(current_user, :remote => true) do |f| %>
<p>
<%= f.label :language, t("language") %>:
<%= f.select :language, options_for_select([["Русский", "rus"], ["English", "eng"]]) %>
</p>
<p><%= f.submit t "options.save" %></p>
<% end %>
Notice the variable |f| and change of label_tag, select_tag and submit_tag to f.label, f.select and f.submit
In rails form_for and corresponding form_buider object(|f|) are used to group values under a common key, which rails can understand. *_tag helpers are generally used to pass unrelated parameters.
Trying to make a nested form, which is working fine so far, except i need to put some dropdowns for the user to choose, as well as maybe make a couple of validations, however it seems nothing gets out of the form properly and keep getting errors no matter what I try.
three models.
--configuration
has_many :configoptions
accepts_nested_attributes_for :configoptions
--configoption
belongs_to :configuration
has_many :items
and item
belongs_to :configoption
scope :sorted, order('items.position ASC')
Now, so far I'm creating a nested form, looping through the configoptions, but for each option is possible there's more than one item. So I want to make a drop-down for those options where this is the case.
In my view i have:
<p>
<th>Elements</th>
<th>Quantity</th>
</p>
<%= form_for #config, :url => {:action => 'show', :id => #config.id} do |f| %>
<%= f.fields_for :configoptions do |fp| %>
<p>
<% if :items.count > 1 %>
<%= fp.text_field :name %>
<% else %>
<% fp.select(:items, :name)%>
<% end %>
<%= fp.text_field :quantity %>
</p>
<% end %>
<%= f.submit %>
<% end %>
I get an error obviously telling me that it can't count the :items.
How do you think I can make this work?
Thanks!
<%= form_for #config, :url => {:action => 'show', :id => #config.id} do |f| %>
<%= f.fields_for :configoptions do |fp| %>
<%= fp.text_field :id %>
<%= fp.text_field :name %>
<%= fp.text_field :quantity %>
<% end %>
<%= f.submit %>
<% end %>
OK, I think I figured it out, at least it seems to be doing what I want now.
I modified the view to pass the instance of the configoption into the nested form itself to be able to create the drop downs.
<% for configoption in #config.configoptions %>
<%= f.fields_for :configoptions, configoption do |fp| %>
<p>
<% if configoption.items.count > 1 %>
<%= fp.select (:name, options_from_collection_for_select(configoption.items.sorted, 'name', 'name'))%>
<% else %>
How to create form for new User model with embedded Phone model?
I've found solution for creating form to add Phone for existing User but how to do that at the same time i create new User?
You have to create a nested form
<%= form_for #user, :url => users_path do |f| %>
<%= f.label :name, "Name:" %> <br />
<%= f.text_field :name %>
<%= f.fields_for :phone do |p| %>
<%= p.label :number, "Phone Number" %> <br />
<%= p.text_field :number %>
<% end %>
<% end %>