Rails 3 form helper for models with nested attributes - ruby-on-rails-3

I'm baffled with forms for nested attributes
Here's my model:
class User < ActiveRecord::Base
has_one :user_email_preference, :dependent => :destroy
accepts_nested_attributes_for :user_email_preference
attr_accessible :user_email_preference_attributes
Here's my view with devise:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= f.fields_for resource.user_email_preference do |email| %>
<li>
<h3><%= email.label :alerts %></h3>
<div><%= email.check_box :alerts, class:"js-switch" %></div>
</li>
<li>
<h3><%= email.label :newsletter %></h3>
<div><%= email.check_box :newsletter, class:"js-switch" %></div>
</li>
<% end %>
<% end %>
The view renders correctly with http get. However submitting the form constantly gives me the following error message:
Can't mass-assign protected attributes: user_email_preference
Any suggestions on where went wrong?

It should just be f.fields_for :user_email_preference do |email|. This will set the correct nested attribute name.

Related

ActiveAdmin Form with 2 models (belongs_to and has_many) don't work

I have the following code form my passport_visas.rb model
ActiveAdmin.register PassportVisa do
menu :label => "Visas"
form :partial => "form"
index :title => "Visas"
end
And this is my code for the partial "form"
<%= semantic_form_for [:admin, #passport_visa] do |f| %>
<%= f.inputs "Main information" do %>
<%= f.input :country %>
<%= f.input :citizenship, :as => :radio, :collection => {"US Citizen" => 0, "Foreign National" => 1} %>
<%= f.input :visa_type, :as => :select, :collection => ["Tourist", "Business", "Official"] %>
<%= f.input :visa_required, :label => "Is Visa Required?", :as => :radio, :collection => {"Required" => 0, "Not Required" => 1} %>
<%= f.input :maximum_stay, :label => "Maximum Stay" %>
<% end %>
<fieldset class="actions">
<ol>
<li class="action input_action" id="passport_visa_add_new_entry">
<input name="new_entry" type="button" value="Add New Entry">
</li>
<li class="action input_action" id="passport_visa_remove_entry">
<input name="remove_entry" type="button" value="Remove Entry">
</li>
</ol>
</fieldset>
<!-- Problem -->
<%= f.inputs "Entries" do %>
<%= f.has_many :visa_entries do |entry| %>
<%= entry.input :type_of_entry, :as => :select, :collection => ["Testing"] %>
<% end %>
<% end %>
<%= f.actions %>
<% end %>
And I'm getting this error message: "undefined method `has_many' for #Formtastic::FormBuilder:0x10c234c38"
I'm trying the entire day to setup this form, here's my model's code:
passport_visa.rb
class PassportVisa < ActiveRecord::Base
has_many :visa_entries
accepts_nested_attributes_for :visa_entries
end
visa_entry.rb
class VisaEntry < ActiveRecord::Base
belongs_to :passport_visa
attr_accessible :type_of_entry, :maximum_validity, :embassy_fees, :service_fees, :processing_time
end
Finally I found a work around for that. So here's what I did to make it work:
First I got rid of the _form partial, because for some reason the "has_many" don't work inside there.
I modified my 2 models:
passport_visa.rb
class PassportVisa < ActiveRecord::Base
has_many :visa_entries
accepts_nested_attributes_for :visa_entries
attr_accessible :visa_entries_attributes, :country, :citizenship, :visa_type, :visa_required, :maximum_stay
validates_presence_of :country, :citizenship, :visa_type, :visa_required, :maximum_stay
end
visa_entry.rb
class VisaEntry < ActiveRecord::Base
belongs_to :passport_visa
attr_accessible :type_of_entry
validates_presence_of :type_of_entry
end
And the last part, the actual form:
form do |f|
f.inputs "Entries" do
f.has_many :visa_entries do |ff|
ff.input :type_of_entry, :as => :select, :collection => ["Testing"]
end
end
f.actions
end
Bye!

Nested fields not being created on form

I am trying to get a Project form to build the first (starting) time of several (up to 12) volunteer time blocks.
project.rb
class Project < ActiveRecord::Base
attr_accessible :title, ...
has_many :vol_times, :dependent => destroy
accepts_nested_attributes_for :vol_times, :reject_if => lambda { |a| a[:start_time].blank? }, :allow_destroy => true
...
end
vol_time.rb
class Vol_time < ActiveRecord::Base
attr_accessible :start_time, ...
belongs_to :project
end
ProjectsController
class ProjectsController < ApplicationController
before_filter :signed_in_user, only: :create
...
def new
#project = Project.new
#user = current_user
#project.vol_times.build
end
...
end
Vol_Times Controller
class Vol_TimesController < ApplicationController
def new
#reward = Reward.new
end
...
end
My view looks like this...
<%= form_for(#project) do |f| %>
<div class="form_field_block">
<p class="form_label"> Project Title</p>
<%= f.text_field :title, :size => 40, :placeholder => " Project Title...", :class => "frm" %>
</div>
<div class="form_field_block">
<p class="form_label"> Project Sub-title</p>
<%= f.text_field :sub_title, :size => 40, :placeholder => " Project Sub-title...", :class => "frm" %>
</div>
<p class="clearing"></p>
<div class="form_field_block">
<% f.fields_for :vol_times do |builder| %>
<%= render :partial => 'start_time', :f => builder %>
<% end %>
</div>
<p class="clearing"></p>
<%= button_tag "btn_start_project.png", :class => "btn_save" %>
<% end %>
And the _partial looks like this...
<%= f.label :start_time, "Starting Time" %>
<%= f.text_field :start_time %>
When I view the page, I see the containing <div>, but not the contents of the ERB, which should be parsed from the _partial.
Any ideas why this isn't working? I got the general context from Ryan Bates' RailsCast #196 - Here
you are missing a = on the fields_for. It should be
<%= f.fields_for :vol_times do |builder| %>

Rails 3 fields_for has_one not rendering form fields with devise

Hello Im using devise to register users, and I want to create a profile related to a user every time a user signs up, the problem is that when I try to add the full name of the person from the profile model on the registration view for devise's user registration, it does not show up...
This are my models:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
# attr_accessible :title, :body
end
This is the profile model
class Profile < ActiveRecord::Base
attr_accessible :email, :name, :phone, :code
belongs_to :user
validates_presence_of :user
end
And this is the devise view modified:
<% provide(:title, 'Sign up' ) %>
<h2>Sign up</h2>
<div class="row">
<div class="span6 offset3">
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages', object: resource %>
<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.label :full_name %>
<%= profile_form.text_field :name %>
<% end %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
<div class="form-actions">
<%= f.submit "Sign up", class: "btn btn-large btn-primary" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
I cant see anything wrong to why the name text field would not appear...
Thank you for your help
Figured it out, it was so simple I feel bad... so here it is:
I forgot to generate a new profile on the form so it ends up looking kind of like this:
.
.
.
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= render 'shared/error_messages', object: resource %>
<div class="invisible">
<%= resource.build_profile %>
</div>
<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.label :full_name %>
<%= profile_form.text_field :name %>
<% end %>
<%= f.label :email %>
<%= f.email_field :email %>
.
.
.
Hope this solves someone else's problem as well!
Thank you!
This question is already answered, but I thought I'd throw this up as an alternative for others. Form backing objects can be used to go back to a one form/one model kind of setup and help keep code cleaner.
Here's a good writeup on it:
http://pivotallabs.com/form-backing-objects-for-fun-and-profit/

Updating unused field after initial creation does not work rails

For reference, this is using Rails 3.0.9.
I can't seem to figure out why, but I cannot update a field which was left blank at the initial creation of the object/row.
If my model looks like this:
name - string
date - date
message - string
If I put info in all of the fields upon initial creation, everything can be updated without a hitch later on. But if I do not put info in one of the fields, say the title, I cannot update that field after initial creation.
I'm not doing anything out of the ordinary, the form is pretty plain jane:
<%= form_for #event do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :date %><br />
<%= f.date_select :date %>
</p>
<p>
<%= f.label :message %><br />
<%= f.text_field :message %>
</p>
<p><%= f.submit %></p>
<% end %>
And my controller is equally as simple:
def update
if #event.update_attributes(params[:event])
redirect_to #event, :notice => "Successfully updated event."
else
render :action => 'edit'
end
end
And the model:
class Event < ActiveRecord::Base
has_many :races, :dependent => :destroy
has_many :price_groups, :dependent => :destroy
accepts_nested_attributes_for :races, :reject_if => lambda{ |a| a[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :price_groups, :reject_if => lambda{ |a| a[:name].blank? }, :allow_destroy => true
attr_accessible :name, :date, :message, :races_attributes, :price_groups_attributes
end

Nested forms fields_for :prices, repeat forms few time

i have many to many throught asociation and then i do this
fields_for :device
this displaying in good way, but i cannot save it i get unknown attribute :price
And fields_for :devices
And so on, one device makes one more repeat, if i write f.fields_for :price it gives good text field count, but it write
unknown attribute: price
Rails.root: C:/Users/Ignas/mildai/baze4
Application Trace | Framework Trace | Full Trace
app/controllers/commercial_offers_controller.rb:74:in `block in update'
app/controllers/commercial_offers_controller.rb:73:in `update'
thank you
additional information:
controller
def edit_prices
#commercial_offer = CommercialOffer.find(params[:id])
end
link to edit prices
<%= link_to "Edit prices", :controller => "CommercialOffers", :action => "edit_prices", :id => #commercial_offer %>
_edit_price.html.erb
<%= form_for #commercial_offer do |f| %>
<% CommercialOffer.find(params[:id]).devices.each do |device| %>
<%= check_box_tag "commercial_offer[device_ids][]", device.id, #commercial_offer.devices.include?(device) %>
<%= device.name %>
<%= f.fields_for :prices do |builder| %>
<%= render 'prices/new_price', :f => builder, :commercial_offer => #commercial_offer, :device => device %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit "Save"%>
</div>
<% end %>
for one device it have render only one time, but it rendering for one device such times how many devices is with same commercial_offer_id
_new_price.html.erb
Price:
<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id, :value => device.id %></br>
class CommercialOffer < ActiveRecord::Base
has_many :prices
has_many :devices, :through => :prices
accepts_nested_attributes_for :prices
accepts_nested_attributes_for :devices
end
class Device < ActiveRecord::Base
has_many :prices
accepts_nested_attributes_for :prices
has_many :commercial_offer, :through => :prices
class Price < ActiveRecord::Base
belongs_to :device
belongs_to :commercial_offer
end
There is no any field_for method.
Also your question isn't clear. What actually you want to do? Show only one device? Wich one? First, last?
UPD
commercial_offer.rb:
class CommercialOffer < ActiveRecord::Base
has_many :prices
has_many :devices, :through => :prices
accepts_nested_attributes_for :prices
accepts_nested_attributes_for :devices, :allow_destroy => true
end
Your view
<%= form_for #commercial_offer do |f| %>
<%= f.fields_for :devices do |device| %>
<p>
<%= device.check_box :_destroy %>
<%= device.label "Destroy?" %>
</p>
<p>
<%= device.text_field :name %>
</p>
<%= device.fields_for :prices do |builder| %>
<%= render 'prices/new_price', :f => device %>
<% end %>
<% end %>
<% end %>
<div class="actions">
<%= f.submit "Save"%>
</div>
<% end %>
_new_price partial
<%= f.text_field :price %></br>
Quantity:
<%= f.text_field :quantity %></br>
Device id:
<%= f.text_field :device_id %></br>