在对/:username/about执行post操作时,我将收到“警告:无法批量分配受保护的属性:about”。

class About < ActiveRecord::Base
  belongs_to :user
  accepts_nested_attributes_for :user
  attr_accessible :user_id, :sexuality, :relationship_status, :living_with, :religion, :web, :languages
  attr_accessible :appearance_ethnicity, :appearance_height, :appearance_weight, :appearance_hair, :appearance_piercings, :appearance_tattoo, :appearance_eyes_color
  attr_accessible :interest_list, :music_list, :movie_list, :book_list, :tv_list
  validates_presence_of :user_id
end

class User < ActiveRecord::Base
  ...
  accepts_nested_attributes_for :about
end

class AboutsController < ApplicationController
  def update
    @user = User.first(:conditions => ["lower(username) = ?", params[:username].downcase]) if true
    @about = @user.about
    if @about.update_attributes(params[:about])
      flash[:notice] = "Successfully updated post."
      respond_with(@about, :location => about_path(@about.user.username))
    else
      redirect_to :edit
    end
  end
end

我得到了
 Started POST "/heaven/about" for 127.0.0.1 at Tue Mar 01 02:57:08 -0200 2011
 Processing by AboutsController#update as HTML
 Parameters: {"commit"=>"Update Profile", "authenticity_token"=>"7/Xcv0z07A3JvsTiDlapoghi25sIjgiFfhN33hFWfoU=", "utf8"=>"✓", "username"=>"roses", "about"=>{"about"=>{"sexuality"=>"not_specified", "book_list"=>"", "relationship_status"=>"not_specified", "appearance_eyes_color"=>"not_specified", "music_list"=>"", "appearance_height"=>"", "living_with"=>"not_specified", "movie_list"=>"", "tv_list"=>"", "appearance_hair"=>"not_specified", "religion"=>"not_specified", "web"=>"", "interest_list"=>"", "appearance_weight"=>"", "appearance_piercings"=>"", "appearance_tattoo"=>""}}}
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  User Load (0.8ms)  SELECT "users".* FROM "users" WHERE (lower(username) = 'heaven') LIMIT 1
  About Load (0.5ms)  SELECT "abouts".* FROM "abouts" WHERE ("abouts".user_id = 2) LIMIT 1
WARNING: Can't mass-assign protected attributes: about
CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Redirected to http://localhost:3000/heaven/about
Completed 302 Found in 832ms

谢谢。

最佳答案

您需要设置attr_accessible :about_attributes

关于ruby-on-rails - 警告:无法批量分配 protected 属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5150683/

10-13 05:02