I'm getting a mass assignment error when submitting a nested form for a has_one polymorphic model. The form is trying to create Employee and Picture instances based on the polymorphic association Rails guide.
I would appreciate literally ANY functioning example of a nested creation form for a has_one polymorphic model! I know there are tons of questions on mass assignment errors but I've never seen a working example with polymorphic associations.
Models
class Picture < ActiveRecord::Base
belongs_to :illustrated, :polymorphic => true
attr_accessible :filename, :illustrated
end
class Employee < ActiveRecord::Base
has_one :picture, :as => :illustrated
accepts_nested_attributes_for :picture
attr_accessible :name, :illustrated_attribute
end
Migrations
create_table :pictures do |t|
t.string :filename
t.references :illustrated, polymorphic: true
end
create_table :employees do |t|
t.string :name
end
controllers/employees_controller.rb
...
def new
#employee = Employee.new
#employee.picture = Picture.new
end
def create
#employee = Employee.new(params[:employee])
#employee.save
end
...
Error
Can't mass-assign protected attributes: illustrated
app/controllers/employees_controller.rb:44:in `create'
{"utf8"=>"✓", "authenticity_token"=>"blah"
"employee"=>{"illustrated"=>{"filename"=>"johndoe.jpg"},
"name"=>"John Doe"},
"commit"=>"Create Employee"}
In the models, I've tried every permutation of :illustrated, :picture, :illustrated_attribute, :illustrated_attributes, :picture_attribute, :picture_attributes, etc. Any tips or examples?
EDIT:
_form.html.erb
<%= form_for(#employee) do |f| %>
<%= f.fields_for :illustrated do |form| %>
<%= form.text_field :filename %>
<% end %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
You need to specify the nested_attributes appropriately. accepts_nested_attributes_for takes the name of the association as parameter and then you need to add the same assoc_attributes to your attr_accessible. So change your Employee model to
class Employee < ActiveRecord::Base
has_one :picture, :as => :illustrated
accepts_nested_attributes_for :picture
attr_accessible :name, :picture_attribute
end
And change the field_for line in the view code to
<%= f.fields_for :picture do |form| %>
Related
I am working on hand-rolling a user authentication system for fun. I've created two different models, a UserAccount and UserCredential the idea being that credentials such as email, username, profile pic, etc are separate from the actual account.
In order to create a UserCredential when a UserAccount is created I am using accepts_nested_attributes_for. I am running into some issues saving the UserCredential when the UserAccount saves.
Migration:
create_table :user_accounts, id: :uuid do |t|
t.references :user_credentials
...
end
add_reference :user_credentials, :user_account, type: :uuid, null: false, foreign_key: true
user_account.rb
has_one :user_credential, inverse_of: :user_account, dependent: :destroy
...
accepts_nested_attributes_for :user_credential
validates_associated :user_credential
user_credential.rb
class UserCredential < ApplicationRecord
validates :email, uniqueness: true
belongs_to :user_account, inverse_of: :user_credential
validates_presence_of :user_account
accounts_controller.rb
def new
#user = UserAccount.new
end
def create
#user = UserAccount.create!(user_params)
end
private
def user_params
params.require(:user_account).permit(user_credentials: [:email])
# I'm not sure if `user_credential` should be plural here or not?
end
_form.html.erb
<%= form_with model: #user, local: true do |form| %>
<%= form.fields_for :user_credentials do |u| %>
<div class="form-control">
<%= u.label :email %>
<%= u.email_field :email %>
</div>
<% end %>
...
<% end %>
Two small things I've noticed:
if I change the params and the fields_for to user_credential (removing the plural) the email field disappears.
If I keep the params as is I get this exception: ActiveModel::UnknownAttributeError (unknown attribute 'user_credentials' for UserAccount.):
I've seen people recommending adding a #user.user_credential.build() to the new method but doing that just gives me a nil:NilClass error.
I guess you need to change user_credentials to user_credentials_attributes
def user_params
params.require(:user_account).permit(user_credentials_attributes: [:email])
end
When nested attributes are submitted through a form, the _attributes tag is appended for the nested attributes parent.
This is a new error to me, and struggling to resolve it. It also states: Roaster(#70130698993440) expected, got "1" which is an instance of String(#70130675908140)
It's highlighting my create method in my Roasts Controller:
def create
#roast = Roast.new(roast_params)
The scenario is that I'm trying to create a triple nested form. for three models Roasts Countries and Regions where roasts has many countries and countries has many regions.
I'm assuming there is something wrong with the roast params, but I can see what it is. I have added the associations there for the nested models
def roast_params
params.require(:roast).permit(:roaster, :name, :bestfor, :beans, :roast, :tastingnotes, :notes, :slug, :avatar, :countries_attributes => [:country_name, :regions_attributes => [:region_name]])
end
my form
<div class="form-group">
<%= form.fields_for :countries do |countries_form| %>
<%= countries_form.label :country %>
<%= countries_form.text_field :name, class: "form-control" %>
</div>
<div class="form-group">
<%= form.fields_for :regions do |regions_form| %>
<%= regions_form.label :region %>
<%= regions_form.text_field :region_name, class: "form-control" %>
<% end %>
<% end %>
</div>
Roast Controller
...
def new
#roast = Roast.new
#roast.countries.build.regions.build
end
...
roast model
class Roast < ApplicationRecord
has_many :tastings
has_many :countries
has_many :notes, through: :tastings
has_many :comments, as: :commentable
belongs_to :roaster
accepts_nested_attributes_for :countries
country model
class Country < ApplicationRecord
has_many :regions, inverse_of: :country
accepts_nested_attributes_for :regions
belongs_to :roasts
region model
class Region < ApplicationRecord
belongs_to :country
I've nested the regions params in the country params, is that correct? I also saw on SO other issues with suggestions for setting config.cache_classes to true in development.rb but that didn't help here.
Update
So looking at this further, I believe it's not related to the nested forms, but rather a collection_select I'm using.
<%= form.label :roaster, class: 'control-label' %>
<%= form.collection_select(:roaster, Roaster.order(:roaster_name).all, :id, :roaster_name, prompt: true, class: "form-control") %>
So this select is pulling the roaster_name from a model called Roaster.
My params now look like the below:
params.require(:roast).permit(:roaster_name, :roaster, :name, :bestfor, :beans, :roast, :tastingnotes, :notes, :slug, :avatar, :countries_attributes => [:country_id, :country_name, :regions_attributes => [:region_id, :region_name]])
And looking at the console when submitting the form, it seems that just the :id of Roaster is getting passed, rather than the value of :roaster_name.
{"utf8"=>"✓",
"authenticity_token"=>"EG+zty85IiVsgipm1pjSAEZ7M66ELWefLq8Znux+cf89sSnVXxielRr1IaSS9+cJvdQD8g1D4+v2KqtKEwh6gw==",
"roast"=>{"roaster"=>"1", "name"=>"Espress", "countries_attributes"=>{"0"=>{"country_name"=>"UK"}}, "regions"=>{"region_name"=>"Highlands"}, "bestfor"=>"", "roast"=>"", "tastingnotes"=>""},
"commit"=>"Create Roast"}
Can't work this out
ActiveRecord::AssociationTypeMismatch is raised when an association-setter (Roast#roaster= in this case) is called with a value that is not an instance of the expected class. Roaster was expected, got String.
The issue seems to be with passing roaster in as a param, which is "1" (String) in your example. I'm guessing this is actually an ID of a Roaster, the form code in the question does not show it.
Perhaps you meant to permit and pass a roaster_id param?
def roast_params
params.require(:roast).permit(:roaster_id, # ...
end
How do I build a nested form for objects using multiple table inheritance in rails? I am trying to make a nested form to create an object using a model with a has_many relationship to another set of models that feature multi-table inheritance. I am using formtastic and cocoon for the nested form and the act_as_relation gem to implement the multiple table inheritance.
I have the following models:
class Product < ActiveRecord::Base
acts_as_superclass
belongs_to :store
end
class Book < ActiveRecord::Base
acts_as :product, :as => :producible
end
class Pen < ActiveRecord::Base
acts_as :product, :as => :producible acts_as :product, :as => :producible
end
class Store < ActiveRecord::Base
has_many :products
accepts_nested_attributes_for :products, :allow_destroy => true, :reject_if => :all_blank
end'
For this example, the only unique attribute that book has compared to other products is an author field. In reality, I have a number of unique attributes for book which is why I chose multi-table inheritance over the more commonplace single table inheritance.
I am trying to create a nested form that allows you to create a new store with products. Here's my form:
<%= semantic_form_for #store do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<h3>Books/h3>
<div id='books'>
<%= f.semantic_fields_for :books do |book| %>
<%= render 'book_fields', :f => book %>
<% end %>
<div class='links'>
<%= link_to_add_association 'add book', f, :books %>
</div>
<% end %>
<%= f.actions :submit %>
<% end %>
And the book_fields partial:
<div class='nested-fields'>
<%= f.inputs do %>
<%= f.input :author %>
<%= link_to_remove_association "remove book", f %>
<% end %>
</div>
I get this error:
undefined method `new_record?' for nil:NilClass
Based on reading the issues on the github page for act_as_relation, I thought about making the relationship between store and books more explicit:
class Product < ActiveRecord::Base
acts_as_superclass
belongs_to :store
has_one :book
accepts_nested_attributes_for :book, :allow_destroy => true, :reject_if => :all_blank
end
class Book < ActiveRecord::Base
belongs_to :store
acts_as :product, :as => :producible
end
class Store < ActiveRecord::Base
has_many :products
has_many :books, :through => :products
accepts_nested_attributes_for :products, :allow_destroy => true, :reject_if => :all_blank
accepts_nested_attributes_for :books, :allow_destroy => true, :reject_if => :all_blank
end
Now, I get a silent error. I can create new stores using the form, and cocoon allows me to add new book fields, but when I submit the store gets created but not the child book. When, I go through the `/books/new' route, I can create a new book record that spans (the products and books table) with no problem.
Is there a workaround to this problem? The rest of the code can be found here.
Maybe you could:
Build the books relation manually on your stores_controller#new action
#store.books.build
Store manually the relation on you stores_controller#create action
#store.books ... (not really confident on how to achieve it)
Keep us posted.
You might want to consider creating your own form object. This is a RailsCast pro video, but here are some of the examples in the ASCIIcast:
def new
#signup_form = SignupForm.new(current_user)
end
This signup form can include relations to your other objects, just as you would in your original controller code:
class SignupForm
# Rails 4: include ActiveModel::Model
extend ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Validations
validates_presence_of :username
validates_uniqueness_of :username
validates_format_of :email, with: /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/
validates_length_of :password, minimum: 6
def persisted?
false
end
def subscribed
subscribed_at
end
def subscribed=(checkbox)
subscribed_at = Time.zone.now if checkbox == "1"
end
def generate_token
begin
self.token = SecureRandom.hex
end while User.exists?(token: token)
end
end
Here is the link to the RailsCast. Getting a pro membership might be worth your time. I have been getting lucky with a membership through www.codeschool.com where you can get 'prizes' when you finish courses:
RailsCast:
http://railscasts.com/episodes/416-form-objects
I am not sure how to proper create a rails form with nested form. I have followed many tutorial but becoming more confused has in what should it be, singular plurials, controller... Here my models
model/event.rb
attr_accessible :description :title, :tag_ids, :locations_attributes
has_many :location
accepts_nested_attributes_for :location, :allow_destroy => true
model/location.rb
attr_accessible :address, :customer_id, :event_id, :latitude, :longitude
belongs_to :customer
belongs_to :event
controller.rb
def new
#event = Event.new
...
def create
#event = Event.new(params[:event])
...
view form.html.erb
<%= form_for(#event) do |f| %>
<%= f.fields_for :locations do |e| %>
<%= e.text_field :longitude %>
<%= e.text_field :latitude %>
<% end %>
...
<% end %>
error
Can't mass-assign protected attributes: locations
params send
"locations"=>{"longitude"=>"45.6666",
"latitude"=>"47.44444665"},
Either my relationship are wrong because fields_for doesn't support it, either my controller is not proper, or either rails is just not a great language, or i don't understand it anymore.
You.re nearly there...
event.rb - locations NOT location
attr_accessible :description :title, :tag_ids, :locations_attributes
has_many :locations
accepts_nested_attributes_for :locations, :allow_destroy => true
Should do it I think
edit
And as Valery Kvon says, you need to add
#event.locations.build
to your controller
Edward's answer +
def new
#event = Event.new
#event.locations.build
end
I'm having issues with a form in a custom registration for Devise. This form needs to have both User and Business model information in there that I'll be creating both at once on the form. It's important that this can't be a multi-step registration form - I need to have both the User and Business models filled out simultaneously, as part of the requirements.
I have the following models:
user.rb
class User < ActiveRecord::Base
has_many :businessusers, :include => :business
has_many :businesses, :through => :businessusers
accepts_nested_attributes_for :businessusers
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :role, :businessusers
business.rb
class Business < ActiveRecord::Base
has_many :businessusers
has_many :users, :through => :businessusers
businessuser.rb
class Businessuser < ActiveRecord::Base
belongs_to :business
belongs_to :user
end
businessusers migration
create_table :businessusers do |t|
t.integer :user_id
t.integer :business_id
end
In my registration form, I have the following code:
Business Information
<br>
<%= f.fields_for :businessusers do |business_form| %>
<%= business_form.input :name %>
<%= business_form.input :address_line1 %>
<%= business_form.input :address_line2 %>
<%= business_form.input :city %>
Finally, here is my controller:
custom_registration_controller.rb
def create
#user=User.new(params[:user])
#business = #user.businesses.build(params[:business]) unless params[:business] [:name].blank?
end
EDIT: I've gotten past the mass assign attribute problem, but now I have another one.
Does anyone know where I'm going wrong? Before, I was disabling id on businessusers table. But this gave me an error saying I needed a primary ID on the table. So when I allow the field :id, it makes it so that the form no longer has the business fields on it.
Thanks!