2014-09-10 74 views
0

這個問題有很多次被其他用戶詢問,但是仍然沒有解決我的問題。我的rails應用程序出現錯誤「Unpermitted Parameters:profile」出現問題。不允許的參數:profile(NestedAttributes) - RAILS 4

user_controller.rb

class Admin::UsersController < ApplicationController 
    before_filter :set_user, only: [:edit, :update] 
    before_filter :store_location, only: [:index] 
    before_filter :require_admin 

    def edit 
    if @user 
     render 
    else 
     redirect_to admin_users_path, notice: "User profile not found." 
    end 
    end 

    def update 
    # Rails.logger.debug "===> (1)" 
    if @user.update(user_params) 
     redirect_to edit_admin_user_path, notice: "#{@user.profile.full_name} account has been updated." 
    else 
     render 'edit' 
    end 
    end 

    private 

    def set_user 
    @user = User.find(params[:id]) 
    end 

    def user_params 
    params.require(:user).permit(:id, :username, :email, profile_attributes: [:user_id, :full_name]) 
    end 
end 

edit.html.erb

<%= form_for :user, url: admin_user_path(@user), method: :patch do |f| %> 

    <div class="form-group"> 
    <%= f.label :username %><br> 
    <%= f.text_field :username, :class => "form-control" %> 
    </div> 

    <%= f.fields_for :profile, @user.profile do |profile| %> 

    <div class="form-group"> 
    <%= profile.label :full_name %><br> 
    <%= profile.text_field : full_name, :class => "form-control" %> 
    </div> 

    <% end %> 

    <div class="form-group"> 
    <%= f.submit "Save", :class => "btn btn-primary" %> 
    </div> 

<% end %> 

User.rb

class User < ActiveRecord::Base 

    has_one :profile 
    accepts_nested_attributes_for :profile #, update_only: true, allow_destroy: true 

    validates :username, :uniqueness => { :case_sensitive => false } 

end 

Profile.rb

class Profile < ActiveRecord::Base 

    belongs_to :user 

    validates_presence_of :user_id 
    validates_presence_of :full_name 

end 

development.log

Started PATCH "/master/users/7" for 127.0.0.1 at 2014-09-10 23:18:26 +0800 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"23oUfOBaYAmcrfhW3R11F1x53lJAT760Shv0HqkmEzw=", "user"=>{"username"=>"lisa", "profile"=>{"full_name"=>"Evalisa Andriaasdasda"}}, "commit"=>"Save", "id"=>"7"} 
[1m[35mUser Load (0.3ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 7 LIMIT 1 
[1m[36mUser Load (0.3ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 6 ORDER BY `users`.`id` ASC LIMIT 1[0m 
Unpermitted parameters: profile 
[1m[35m (0.2ms)[0m BEGIN 
[1m[36mUser Exists (0.4ms)[0m [1mSELECT 1 AS one FROM `users` WHERE (`users`.`username` = 'lisa' AND `users`.`id` != 7) LIMIT 1[0m 
[1m[35m (0.2ms)[0m COMMIT 
[1m[36mProfile Load (0.4ms)[0m [1mSELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 7 LIMIT 1[0m 

我真的不知道哪裏是錯誤的。請幫忙。

提前致謝!

回答

2

更新您的user_params方法以包含配置文件對象中的所有屬性。你缺少id所以Rails是試圖創建配置文件對象,這會導致意外的行爲(你的開發日誌顯示你正在修補請求,所以你要更新的東西,而不是創造的東西):

def user_params 
    params.require(:user).permit(:id, :username, :email, profile_attributes: [:id, :user_id, :full_name]) 
end 

您可能還需要更新您的表單才能真正通過profile對象的id

看到這個答案再看看:https://stackoverflow.com/a/17561608/1026898

2

啊,我解決我自己的問題。對於那些在那裏,這裏是解決方案: -

從development.log,我注意到一些是"profile" => {}。它應該是"profile_attributes" => {}所以我修改了edit.html.erb形式: -

<%= form_for :user, url: admin_user_path(@user), method: :patch do |f| %> 

    <div class="form-group"> 
    <%= f.label :username %><br> 
    <%= f.text_field :username, :class => "form-control" %> 
    </div> 

    <%= f.fields_for :profile_attributes, @user.profile do |profile| %> 

    <div class="form-group"> 
    <%= profile.label :full_name %><br> 
    <%= profile.text_field :full_name, :class => "form-control" %> 
    </div> 

    <% end %> 

    <div class="form-group"> 
    <%= f.submit "Save", :class => "btn btn-primary" %> 
    </div> 

<% end %> 

然後,我得到了另一個錯誤development.log說: -

ActiveRecord::RecordNotSaved (Failed to remove the existing associated profile. The record failed to save after its foreign key was set to nil.): 

所以,再一次更新我的user.rb有: -

class User < ActiveRecord::Base 

    has_one :profile 
    accepts_nested_attributes_for :profile, update_only: true, allow_destroy: true 

    validates :username, :uniqueness => { :case_sensitive => false } 
end