2012-07-09 3 views
0

내 모델에 accepts_nested_attributes_for를 추가 했는데도 오류가 발생했습니다.
여전히 "보호 된 속성을 대량 지정할 수 없습니다"라고 말합니다
이 문제를 방지하려면 무엇을해야합니까?"보호 된 속성을 대량 지정할 수 없습니다"오류가 발생하지 않도록하는 방법

모델/user.rb

class User < ActiveRecord::Base 

    validates_presence_of :username 
    validates_uniqueness_of :username 
    validates_length_of :username, :within => 4..10 

    acts_as_messageable 

    has_one :user_profile 
    accepts_nested_attributes_for :user_profile 

    # Include default devise modules. Others available are: 
    # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, :confirmable, 
     :recoverable, :rememberable, :trackable, :validatable 

    # Setup accessible (or protected) attributes for your model 
    attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :user_profile_attributes 

    def mailboxer_email(message) 
    email 
    end 

# def name 
# email 
# end 

end 

모델/user_profile.rb

class UserProfile < ActiveRecord::Base 
belongs_to :user 
accepts_nested_attributes_for :user 
attr_accessible :nickname 
end 

조회/등록/edit.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2> 

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> 
    <%= devise_error_messages! %> 

    <div class="field"> 
    <%= f.label :nickname %><br /> 
    <%= f.fields_for :nickname_attributes, @user.user_profile do |user_profile| %> 
    <%= user_profile.text_field :nickname %> 
    <% end %> 
    </div> 

    <div><%= f.label :email %><br /> 
    <%= f.email_field :email %></div> 

    <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> 
    <%= f.password_field :password %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></div> 

    <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 
    <%= f.password_field :current_password %></div> 

<%= recaptcha_tags :display => {:theme => 'red'} %> 

    <div><%= f.submit "Update" %></div> 
<% end %> 

<h3>Cancel my account</h3> 

<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p> 

<%= link_to "Back", :back %> 

답변

1

attr_accessible 원하는 속성을 정의 사용자가 대량 할당 할 수 있어야합니다. 그냥 거기에 원하는 모든 특성을 가지고 있는지 확인하십시오.

공평하게하려면 걱정하지 않으시면 attr_accessible을 제거하면 오류가 사라지지만 모든 모델 필드는 대량 할당 할 수 있습니다. edit.html.erb에서

+0

감사합니다,하지만 내 경우에는 도움이되지 않았다가 :(보인다는 모든 I 때문에 gem 'Devise'를 사용합니다.이 링크 에 따르면 @ user.build_user_profile을 추가해야하는 것처럼 보입니다.하지만 문제는 무엇입니까? devis는 user_controller를 만들지 않으므로 파일이 없습니다. o 편집. – MKK

1

잘못된 :

f.fields_for :nickname_attributes, 

올바른 :

f.fields_for :user_profile_attributes, 
관련 문제