0

I have got a Products class,Products are visible to zero or many roles . so i have created a polymorphic model called content_roles,which stores the id of the role and content_id (which will be product_id,or event_id),and content_type(product,event etc).

I am using nested_form gem for accepting the role id(using check_box) to store the product and role relation in content_role

the Issue I am facing is I am not able to create a content_role record . in my logs i get unpermitted parameters : role_id

Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxxxxxxxxxxxxxxdLH99ZWLrf8dgT3gcBops=", "product"=>{"product_name"=>"some product", "product_description"=>"some product description", "content_roles_attributes"=>{"role_id"=>["1", "2", ""]}}, "commit"=>"Create Product"}

in my view I have written

 = f.simple_fields_for :content_roles_attributes do |role|
  = role.input :role_id,label: "visible to", as: :check_boxes,label: "Role",collection: Role.all,:required=>true

the controllers permitted params looks like

def product_params
 params.require(:product).permit(:product_description,:product_name,
 content_roles_attributes:  [:role_id,:id],
 multimedia_attributes:[:asset,:_destroy,:id])
end

the product model looks like

 class Product     
  has_many :content_roles, as: :content
  has_many :multimedia ,as: :storable
  # Nested attributes
  accepts_nested_attributes_for :multimedia
  accepts_nested_attributes_for :content_roles
 end

and this is the content_role model

class ContentRole < ActiveRecord::Base
 belongs_to :content, polymorphic: true
 belongs_to :role
 belongs_to :news
 belongs_to :product
end
level0
  • 323
  • 1
  • 3
  • 13

1 Answers1

0

Try changing your product_params to:

def product_params
  params.require(:product).permit(
    :product_description,
    :product_name,
    content_roles_attributes:  [:id, role_id: []],
    multimedia_attributes: [:asset, :_destroy,:id]
  )
end
BroiSatse
  • 44,031
  • 8
  • 61
  • 86
  • as suggested I tried setting role_id to an array ,it gives me a TypeError (no implicit conversion of String into Integer). any suggestion how to solve that?? – level0 Oct 07 '14 at 18:36
  • @Adil - This is now another question, you will need to update your current question with an exact error message together with a backtrace and relevant code (most likely controller action) – BroiSatse Oct 07 '14 at 18:40