2013-07-27 9 views
7

Ho cercato un po 'di tempo, ma non ho trovato la soluzione. Qui sono i miei modelli:Impossibile assegnare l'attributo di massa agli attributi protetti. Errore durante l'uso del modulo nidificato

web.rb

class Web < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
    :recoverable, :rememberable, :trackable, :validatable 

    attr_accessible :email, :password, :password_confirmation, :user_type, :remember_me 

    belongs_to :role, :polymorphic => true 
end 

user.rb

class User < ActiveRecord::Base 
has_one :web, :as => :role 
attr_accessible :dob, :fname, :lname 
end 

org.rb

class Org < ActiveRecord::Base 
    has_one :web, :as => :role 
    attr_accessible :name, :website 
end 

Tutto sembra bene fino uso il simple_form_for anziché form_for normale nel concepire/registrazione/new.html.erb

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :class => 'form-horizontal' }) do |f| %> 

    <%= f.input :email, label: false, :input_html => { :class => "span6", placeholder: "Email", type: "email", required: true}%> 

    <%= f.input :password, label: false, :input_html => { :class => "span6", placeholder: "Password", type: "password" }%> 

    <%= f.input :password_confirmation, label: false, :input_html => { :class => "span6", placeholder: "Re-enter Password", type: "password" }%> 

    <%= f.input :user_type, as: :hidden, :input_html => { :value => user_type} %> 

    <%= f.simple_fields_for resource.role do |rf| %> 
    <%= render :partial => "#{child_class_name.underscore}_fields", :locals => { :f => rf } %> 
    <% end %> 
    <%= f.submit "Sign up" %> 
<% end %> 

La parte nesting mette il parziale con nome model_fields appropriate che contiene campi corrispondenti.

* * _org_fields.html.erb

<%= f.text_field :name, :class=>"span6", :type=>"text", :placeholder=>"Name", :required=>"" %><br /> 
<%= f.text_field :website, :class=>"span6", :type=>"text", :placeholder=>"Website", :required=>"" %> 

Il problema è con il f.simple_fields_for, se tolgo simple_ tutto funziona bene. Ma non voglio che venga rimosso. L'errore che incontro è:

ActiveModel::MassAssignmentSecurity::Error in Devise::RegistrationsController#create 

Can't mass-assign protected attributes: org 

I parametri della richiesta sono:

{"utf8"=>"✓", 
"authenticity_token"=>"NnsyNdrrKJmd8QutqVs6HqZi0EnQmAmZF7zGYqnu+rI=", 
"web"=>{"email"=>"", 
"password"=>"[FILTERED]", 
"password_confirmation"=>"[FILTERED]", 
"user_type"=>"org", 
"org"=>{"name"=>"", 
"website"=>""}}, 
"commit"=>"Sign up"} 

prega di aiuto.

+0

Quale versione di binari stai usando? – Philip7899

+0

@ Philip7899 Non so di OP, ma sto usando le rotaie 3.2.13 – GeekToL

risposta

1

In Web, aggiungere:

attr_accessible :role_attributes 
accepts_nested_attributes_for :role 

Edit: In origine aveva come User ma Elaborare risorsa è Web.

Modifica2: mancava il as: :role. Modificato i valori attr per riflettere.

+0

Questo non ha aiutato. Ho ancora lo stesso problema. – sushilthe

+0

La mia risposta originale li aveva sotto Utente, non sul Web. Hai provato su Web? –

+0

Ora ho questo: Nessuna associazione trovata per nome "org". È stato già definito? – sushilthe

Problemi correlati