打印

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不能由非主键来更新?

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

我的php?name=rails" onclick="tagshow(event)" class="t_tag">rails版本是2.1.0

[ 本帖最后由 chenillen 于 2008-7-10 10:36 编辑 ]

TOP

2008-12-02 10:26 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61