2012-07-29 40 views
1

我接受,即使我有田野以下错误在attr_accessible更新属性导致质量分配例外,甚至attr_accessible

Can't mass-assign protected attributes: utf8, _method, authenticity_token, profile, commit, action, controller, id 

更新我猜,我不想其他属性保存正在引发异常,但我怎样才能滤除它们?

这是params哈希表

{"utf8"=>"✓", 
"_method"=>"put", 
"authenticity_token"=>"1aabj2DxleZoDu/U0SzGXSZrPcesRKXkIXTRVbk9f0A=", 
"profile"=>{"name"=>"Aaron Dufall", 
"company"=>"Supreme Windows", 
"location"=>"", 
"professional_bio"=>""}, 
"commit"=>"Update", 
"id"=>"1"} 

profiles_controller.rb

class ProfilesController < ApplicationController 
    respond_to :html 

    def edit 
     @profile = Profile.find(params[:id]) 
     respond_with @profile 
    end 

    def update 
     @profile = Profile.find(params[:id]) 
     if @profile.update_attributes(params) 
      flash[:success] = "Profile sucessfully updated" 
      redirect_to root_path 
     else 
      flash[:error] = "Profile failed to update" 
      render 'edit' 
     end 
    end 
end 

profile.rb

​​

回答

2

在你的控制器,你应该使用

if @profile.update_attributes(params[:profile]) 

这将只过滤params上“profile”键下的属性。

相关问题