打印

Mass assignment问题

Mass assignment问题

我有两个模型,user和profile.

user是在restful authentication里面生成的,profile是user的子类,他两个的关系是user has_one profile
user.rb内代码是:


Ruby代码



  • has_one :profile
  • attr_accessible :login, :email, :password, :password_confirmation

profile.rb代码是:

Ruby代码



  • class Profile < ActiveRecord::Base  
  •   belongs_to :user
  •   attr_accessible :name, :gender, :birthday, :place, :about_me
  • end

在profile_controller.rb内代码是:

Ruby代码



  • class ProfilesController < ApplicationController  
  •   before_filter :login_required

  •   def edit  
  •     @user = User.find(current_user)  
  •     @profile = Profile.find(params[:user_id])  
  •   end

  • def update  
  •     @user = User.find(current_user)  
  •     @profile = @user.profile  
  •     if
    @profile.update_attributes(@profile.attributes)  
  •       flash[:notice] = "Your profile was successfully updated"
  •       redirect_to(user_path(@user))  
  •     else
  •       render :action => 'edit'
  •     end
  •   end

  • end

但是,我在进行更新profile的时候,log出现错误说:


WARNING: Can't mass-assign these protected attributes: updated_at, id, user_id, created_at





但是,这里我已经把attr_accessible申明了啊?

怎么解决这个问题呢?

TOP

你声明了 Profile 里面只有那几个属性可以被批量赋值,但是
复制内容到剪贴板
代码:
@profile.update_attributes(@profile.attributes)
更新了 @profile 的所有属性,当然包括了 id,user_id 这些不允许被批量赋值的属性,自然会报错

另外 @profile.update_attributes(@profile.attributes) 在做什么,把自身的属性重新保存一下?应该是
复制内容到剪贴板
代码:
@profile.update_attributes( params[:profile] )
我就是鸡蛋黄……

TOP

如果是用
复制内容到剪贴板
代码:
@profile.update_attributes(params[:profile])
就会出现这样的错误:

undefined method `stringify_keys!'

比较郁闷…

TOP