accepts_nested_attributes_for with belongs_to polymorphic devise - ruby-on-rails

I have 3 models and this associations for them
class User < ActiveRecord::Base
# devise modules here
attr_accessible :email, :password, :password_confirmation, :remember_me, :rolable_id, :rolable_type
belongs_to :rolable, :polymorphic => true
end
class Player < ActiveRecord::Base
attr_accessible :age, :name, :position
has_one :user, :as => :rolable
end
class Manager < ActiveRecord::Base
attr_accessible :age, :name
has_one :user, :as => :rolable
end
I'm out of the box from rails way to put accepts_nested_attributes_for :rolable on user model and In this accepts_nested_attributes_for with belongs_to polymorphic question I found some solutions for it but all solution not works for me. All solutions, always the same error when I try to create a user
Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"WKCniJza+PS5umMWCqvxFCZaRVQMPZBT4nU2fl994cU=", "user"=>{"email"=>"john#email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "rolable_type"=>"manager", "rolable"=>{"name"=>"john", "age"=>"24"}}, "commit"=>"Sign up"}
Completed 500 Internal Server Error in 143.0ms
NoMethodError (undefined method `primary_key' for ActiveSupport::HashWithIndifferentAccess:Class):
app/controllers/registrations_controller.rb:13:in `new'
app/controllers/registrations_controller.rb:13:in `create'

My mistake, I'm use nested form
<%= f.fields_for :rolable do |rf| %>
....
<% end %>
change to
<%= f.fields_for :rolable_attributes do |rf| %>
....
<% end %>

for polymorphic association you have to maintain generic model Roll like
class User < ActiveRecord::Base
# devise modules here
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :rolls, :as => :rolable
end
class Player < ActiveRecord::Base
attr_accessible :age, :name, :position
has_many :rolls, :as => :rolable
end
class Manager < ActiveRecord::Base
attr_accessible :age, :name
has_many :rolls, :as => :rolable
end
class Roll < ActiveRecord::Base
attr_accessible :rolable_id, :rolable_type
belongs_to :rolable, :polymorphic=> true
end

Related

Rails nested form for complex associations

Problems:
=rfv.text_area :value render void textarea in edit action (but in db this field has value)
can't get array of role_field_values[] in params
My models and associations:
class Participant < ActiveRecord::Base
belongs_to :role
has_many :role_field_values, dependent: :destroy
accepts_nested_attributes_for :role_field_values
end
class Role < ActiveRecord::Base
has_many :role_fields
has_one :participant
end
class RoleField < ActiveRecord::Base
belongs_to :role
has_many :role_field_values
end
class RoleFieldValue < ActiveRecord::Base
belongs_to :participant
belongs_to :role_field
end
My form edit.slim:
=simple_form_for [#project.becomes(Project), #participant] do |f|
=f.error_notification
h4 =#participant.profile.fio
.form-inline
=f.association :role, remote: true
=f.input :status
#role-fields
=fields_for :role_fields_values do |rfv|
-#participant.role.role_fields.each do |role_field|
.form-group
=rfv.hidden_field :role_field_id, value: role_field.id
br
=rfv.label role_field.name
=rfv.text_area :value, class: 'form-control'
.form-actions
= f.button :submit
Use f.fields_for instead of fields_for

Rails 4 unpermitted params error

I am receiving this error when a form submits:
Unpermitted parameters: postal_address_type_faos
I can confirm that the params come in ok but it looks like the postal_address_type_faos is being removed.
This is the params whitelist:
def paper_params
params.require(:paper).permit(:status, :signature_id, :name, :code,
:create_user_id, :update_user_id, :created_at, :updated_at,
phone_numbers_attributes: [:id, :phone_type_id, :area_code, :number, :extension, :create_user_id, :update_user_id, :created_at, :updated_at, :_destroy],
postal_addresses_attributes: [:id, :postal_address_type_id, :country_id, :line_1,:line_2, :line_3, :city, :territory_id, :postal_zip_code,
:address_note, :latitude, :longitude,
:_destroy, :address_verification_status_id, :comment,
postal_address_type_faos_attributes: [:id, :postal_address_id, :postal_address_type_id, :fao, :create_user_id, :update_user_id, :created_at, :updated_at, :_destroy]])
end
Models:
class Paper < ActiveRecord::Base
has_many :paper_postal_addresses
has_many :postal_addresses, through: :paper_postal_addresses
has_many :postal_address_type_faos, through: :postal_addresses
accepts_nested_attributes_for :postal_addresses
accepts_nested_attributes_for :postal_address_type_faos
has_many :phone_number_papers
has_many :phone_numbers, through: :phone_number_papers
accepts_nested_attributes_for :phone_numbers, allow_destroy: true
class PaperPostalAddress < ActiveRecord::Base
belongs_to :paper
belongs_to :postal_address
has_many :postal_address_type_faos, through: :postal_address
class PostalAddress < ActiveRecord::Base
has_many :postal_address_type_faos
has_many :postal_address_types, :through => :postal_address_type_faos
has_many :paper_postal_addresses
has_many :papers, through: :paper_postal_addresses
class PostalAddressTypeFao < ActiveRecord::Base
belongs_to :postal_address_type
belongs_to :postal_address
has_many :paper_postal_addresses, through: :postal_address
class PostalAddressType < ActiveRecord::Base
has_many :postal_address_type_faos
has_many :postal_addresses, through: :postal_address_type_faos
View code from the address partial is something like this
<%= f.fields_for :postal_addresses do |address|%>
//some address stuff here
//then do address fao type stuff (index is populated but not shown here)
<%= f.fields_for :postal_address_type_faos, #paper.postal_addresses[address.index].postal_address_type_faos[index] do |fa| %>
<%= fa.check_box :postal_address_type_id, label: fa.object.postal_address_type.name %>
<%= fa.text_field :fao, label: fa.object.fao %>
<% end %>
<% end %>
Params coming back as:
{"utf8"=>"✓", "authenticity_token"=>"R8ukkukuykuk",
"paper"=>{"carrier_id"=>"", "name"=>"", "status"=>"Active","digital_id"=>"1","signature_id"=>"1", "phone_numbers_attributes"=>{"0"=>{"create_user_id"=>"1568", "phone_type_id"=>"2", "country_phone_code_id"=>"2", "area_code"=>"",
"number"=>"", "extension"=>""}}, "postal_addresses_attributes"=>{"0"=>{"country_id"=>"", "line_1"=>"", "line_2"=>"", "line_3"=>"", "city"=>"", "territory_id"=>"", "postal_zip_code"=>"", "subterritory"=>"", "comment"=>""},
"1"=>{"country_id"=>"", "line_1"=>"", "line_2"=>"", "line_3"=>"", "city"=>"", "territory_id"=>"", "postal_zip_code"=>"", "subterritory"=>"", "comment"=>""},
"2"=>{"country_id"=>"", "line_1"=>"", "line_2"=>"", "line_3"=>"", "city"=>"", "territory_id"=>"", "postal_zip_code"=>"", "subterritory"=>"", "comment"=>""}},
"postal_address_type_faos_attributes"=>{"0"=>{"postal_address_type_id"=>"1", "attention"=>""}, "1"=>{"postal_address_type_id"=>"0", "fao"=>""}, "2"=>{"postal_address_type_id"=>"0", "fao"=>""}, "3"=>{"postal_address_type_id"=>"0",
"fao"=>""}, "4"=>{"postal_address_type_id"=>"0", "fao"=>""}}}, "area_code_required"=>"", "formaction"=>"add_address", "action"=>"create",
"controller"=>"papers"}
Just change view code from
<%= f.fields_for :postal_address_type_faos, #paper.postal_addresses[address.index].postal_address_type_faos[index] do |fa| %>
to
<%= address.fields_for :postal_address_type_faos, #paper.postal_addresses[address.index].postal_address_type_faos[index] do |fa| %>

nested_attributes with simple_field_for getting unpermitted parameters error rails

I have models
class Profile < ActiveRecord::Base
belongs_to :user
has_many :user_interests
has_many :interests, :through => :user_interests
accepts_nested_attributes_for :user_interests
end
class UserInterest < ActiveRecord::Base
belongs_to :profile
belongs_to :interest
end
class Interest < ActiveRecord::Base
has_many :user_interests
has_many :profiles, :through => :user_interests
end
controller
private
def profile_params
params.require(:profile).permit(:first_name, :last_name, :date_of_birth, user_interests_attributes: {:interest => []})
end
view
=#"http://stackoverflow.com/questions/18428871/multi-column-forms-with-fieldsets"
= simple_form_for(#profile, html: {class: 'form-horizontal'}) do |f|
.well.well-lg
%fieldset
%legend Personal Information
.row
.col-sm-4
.form-group
= f.input :first_name, label: 'First Name*'
= f.hint 'No special characters, please!'
.col-sm-4
.form-group
= f.input :last_name, label: 'First Name*'
= f.hint 'No special characters, please!'
.row
.col-sm-4
.form-group
= f.input :date_of_birth, as: :string, 'data-behaviour'=>'datepicker'
%legend Other Information
.row
.col-sm-4
.form-group
= f.simple_fields_for :user_interests, UserInterest.new do |s|
= s.collection_select(:interest, Interest.all, :id, :name,{},{:multiple=>true})
= f.hint 'Please use Ctrl key on your keyboard to select multiple items'
.row
= f.submit
But getting error unpermitted parameters
profile_params
Unpermitted parameters: interest
=> {"first_name"=>"", "last_name"=>"", "date_of_birth"=>"", "user_interests_attributes"=>{"0"=>{}}}
where my params are:
params
=> {"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"7/7sKljbi88cmUOen/WFWzhzV6exE8I8fBnNMA5EELw=",
"profile"=> {"first_name"=>"", "last_name"=>"",
"date_of_birth"=>"",
"user_interests_attributes"=>{"0"=>{"interest"=>"test"}}},
"commit"=>"Update Profile", "action"=>"update",
"controller"=>"profiles", "id"=>"1"}
Please correct me where I am wrong
You forgot to add accepts_nested_attributes_for :interest on UserInterest.
class Profile < ActiveRecord::Base
belongs_to :user
has_many :user_interests
has_many :interests, :through => :user_interests
accepts_nested_attributes_for :user_interests
end
class UserInterest < ActiveRecord::Base
belongs_to :profile
belongs_to :interest
accepts_nested_attributes_for :interest
end
class Interest < ActiveRecord::Base
has_many :user_interests
has_many :profiles, :through => :user_interests
end
You might have to define interest as a valid parameter on strong_parameters definition in your controller.

Attributes not showing on users#show page

users_controller.rb
class ProfilesController < ApplicationController
before_filter :authenticate_user!
def show
#user = User.find(params[:id]) || User.find(current_user.id)
#questions_for_about = #user.questions.for_about.order('id asc')
#questions_for_personality = #user.questions.for_personality.order('id asc')
end
end
user#show.html.erb
<div class="element">
Ethinicity:
<%= #user.ethnicity.present? ? #user.ethnicity.name : "" %>
</div>
<div class="element">
Education:
<span class="select_for_education">
<%= #user.education.present? ? #user.education.name : "" %>
</div>
The user.education.name is just showing the following - Education:
Without showing the users Education in which was chosen on his personal profile using the following select:
<div class="element">
Ethinicity:
<%= best_in_place current_user, :ethnicity_id, :type => :select, collection: Ethnicity.all.map{|e| [e.id, e.name]}, :inner_class => 'education-edit', nil: 'Select Ethnicity' %>
</div>
<div class="element">
Education:
<span class="select_for_education">
<%= best_in_place current_user, :education_id, :type => :select, collection: Education.all.map{|e| [e.id, e.name]}, :inner_class => 'education-edit', nil: 'Select Education' %>
</div>
What am I doing wrong? And how can I get the users education that DOES display on his/her own profile to display in the show page?
Thanks in advanced!
User.rb
class User < ActiveRecord::Base
include PgSearch
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable,
:omniauth_providers => [:facebook, :twitter, :linkedin]
attr_accessible :email, :password, :password_confirmation, :zip, :gender, :remember_me, :first_name, :last_name,
:birthday, :current_password, :occupation, :address, :interests, :aboutme, :profile_image,
:photos_attributes, :age, :education_id, :ethnicity_id, :blurb
has_many :authorizations, :dependent => :destroy
has_many :comments
has_many :events
has_many :photos, as: :attachable
has_many :questions
has_many :sent_messages, class_name: 'Message', foreign_key: :sender_id
has_many :received_messages, class_name: 'Message', foreign_key: :receiver_id
has_one :ethnicity
has_one :education
end
ethnicity.rb
class Ethinicity < ActiveRecord::Base
attr_accessible :name
has_many :users
end
education.rb
class Education < ActiveRecord::Base
attr_accessible :name
has_many :users
end
You're missing belongs_to association for has_many and has_one relation. The belongs_to association definition needs to be defined in the model whose table has the foreign key.
Given your models, although the other way around is imaginable, here is what I think the associations should look like:
# User Model
class User < ActiveRecord::Base
...
belongs_to :ethnicity
belongs_to :education
end
# Ethnicity Model
class Ethinicity < ActiveRecord::Base
attr_accessible :name
has_many :users
end
# Education Model
class Education < ActiveRecord::Base
attr_accessible :name
has_many :users
end

Rails 3 has_many :through Form

Can't figure out why this is not working. First time using :has_many => :through
Keep getting uninitialized constant User::Employmentship
class User < ActiveRecord::Base
has_many :employmentships
has_many :companies, :through => :employmentships
accepts_nested_attributes_for :employmentships, :allow_destroy => true, :reject_if => proc { |obj| obj.blank? }
attr_accessible :email, :password, :password_confirmation, :firstname, :lastname, :username, :role, :company_ids
end
class Company < ActiveRecord::Base
has_many :employmentships
has_many :users, :through => :employmentships
end
/views/users/_form.html.erb
<p>
<%= for company in Company.all do %>
<%= check_box_tag "user[company_ids][]", company.id, #user.companies.include?(company) %>
<%= company.name%>
<% end %>
</p>
EDIT - If I change #user.companies.include?(company) to false i get the form, but nothing updates.
EDIT 2 -
class Employmentship < ActiveRecord::Base
belongs_to :company
belongs_to :user
attr_accessor :company_id, :user_id
end
Where is you employmentship model? has_many_through is for going through another model.

Resources