打印

Cannot mass-assignment 问题

Cannot mass-assignment 问题

两个模型,user和profile。user我使用的restful_authentication插件,跟profile的关系是 user has_one profile
以下是我的代码:

user.rb (部分)
复制内容到剪贴板
代码:
class User < ActiveRecord::Base
        has_one :profile, :dependent => :destroy
        attr_accessible :login, :email, :password, :password_confirmation, :identity_url
end
profile.rb
复制内容到剪贴板
代码:
class Profile < ActiveRecord::Base
  belongs_to :user
  attr_accessible :name, :gender, :birthday, :place, :about_me  
end
profiles_controller.rb
复制内容到剪贴板
代码:
class ProfilesController < ApplicationController
  before_filter :login_required
  
  def edit
    @user = User.find(current_user)
    @profile = @user.profile
  end
  
  def update
    @user = User.find(current_user)
    @profile = @user.profile   
    respond_to do |format|
      if @profile.update_attributes(@user.profile.attributes)
# 如果我在这里使用这个if @profile.update_attributes(params[:profile]),在更新是,log会报错说是:undefined method `stringify_keys!'
        flash[:notice] = 'Your profile was successfully updated.'
        format.html { redirect_to user_url(@user) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @profile.errors, :status => :unprocessable_entity }
      end
    end
  end  
end
view/profile/edit.html.erb
复制内容到剪贴板
代码:
<h2>Edit Your Profile</h2>
<%= error_messages_for :profile %>

<% form_for (:profile, :url => user_profile_path(@user), :html => {:method => :put}, :index => nil) do |f|%>
         <p>
           Name:

           <%= f.text_field :name %>
         </p>
     
         <p>
           Gender:

           <%= f.text_field :gender %>
         </p>
         <p>
           My Place:

           <%= f.text_field :place %>
         </p>
         <p>
           My Birthday:

           <%= f.text_field :birthday %>
         </p>
         <p>
           About Me

           <%= f.text_area :about_me %>
         </p>
<%= submit_tag 'Update' %>
        <% end %>
Routes.rb(部分)
复制内容到剪贴板
代码:
map.resources :users do |users|
    users.resource :profile, :controller => 'profiles', :action => 'edit'
end
如果更新的话,log中就会出现:
Processing ProfilesController#update (for 127.0.0.1 at 2008-07-10 01:47:40) [PUT]
  Session ID: BAh7CToMY3NyZl9pZCIlMmJhMDA4M2ZiN2UzZmEwNmI1YjJmMDJjY2ZlZmJi
ZTk6DnJldHVybl90byINL3VzZXJzLzM6DHVzZXJfaWRpCCIKZmxhc2hJQzon
QWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNoSGFzaHsABjoKQHVzZWR7
AA==--87898f4f7b44a513b3f7fde77a48958d3af47e53
  Parameters: {"commit"=>"Update", "profile"=>[{"name"=>"Allen", "place"=>"Beijing hub", "about_me"=>"YES", "gender"=>"Male", "birthday"=>"1985-07-03"}], "authenticity_token"=>"d658afd6295d956e8c4c82cac3b48362a7c02370", "_method"=>"put", "action"=>"update", "controller"=>"profiles", "user_id"=>"3"}
  User Columns (0.015332)   SHOW FIELDS FROM `users`
  User Load (0.003480)   SELECT * FROM `users` WHERE (`users`.`id` = 3)
  CACHE (0.000000)   SELECT * FROM `users` WHERE (`users`.`id` = 3)
  Profile Load (0.002561)   SELECT * FROM `profiles` WHERE (`profiles`.user_id = 3) LIMIT 1
  Profile Columns (0.010243)   SHOW FIELDS FROM `profiles`
WARNING: Can't mass-assign these protected attributes: updated_at, id, user_id, created_at
  SQL (0.001385)   BEGIN
  SQL (0.000661)   COMMIT
Redirected to http://localhost:3000/users/3
Completed in 0.08706 (11 reqs/sec) | DB: 0.03366 (38%) | 302 Found [http://localhost/users/3/profile]
难道mass assignment不能由非主键来更新?

页面的结果是显示已经更新,但是并没有写入到数据库中。

我的rails版本是2.1.0

TOP

复制内容到剪贴板
代码:
@user = User.find(current_user)
@profile = @user.profile
@profile.update_attributes(@user.profile.attributes)
这样应该是更新了吧 不过是无用的更新
@profile = @user.profile @profile为数据库中取出来的
然后又@profile.update_attributes(@user.profile.attributes)
把自身更新到自身

应该等于什么都没有做吧 但是操作上应该确实是更新了

TOP

attr_accessible :login, :email, :password, :password_confirmation,
attr_accessible :name, :gender, :birthday, :place, :about_me  

删了这两行就好了.

TOP

引用:
原帖由 alan874 于 2008-7-10 21:55 发表
@user = User.find(current_user)
@profile = @user.profile
@profile.update_attributes(@user.profile.attributes)这样应该是更新了吧 不过是无用的更新
@profile = @user.profile @profile为数据库中取出来的
然 ...
谢谢,我已经改好了,是我自己弄错了代码……
form_for里面不能用 :index => nil这个属性。

TOP

引用:
原帖由 superxielei 于 2008-7-11 14:26 发表
attr_accessible :login, :email, :password, :password_confirmation,
attr_accessible :name, :gender, :birthday, :place, :about_me  

删了这两行就好了.
这样岂不是就会变成mass-assignment了吗?招到攻击的漏洞了

TOP