首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >警告:无法批量分配受保护的属性

警告:无法批量分配受保护的属性
EN

Stack Overflow用户
提问于 2011-03-01 13:03:56
回答 4查看 14.3K关注 0票数 15

在向/:username/about发送帖子时,我收到“警告:无法批量分配受保护的属性: 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

谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-03-01 13:41:56

您需要设置attr_accessible :about_attributes

票数 35
EN

Stack Overflow用户

发布于 2011-03-01 13:11:27

确保您试图在about模型的attr_accessible中提到的About模型中更新的所有字段。

票数 5
EN

Stack Overflow用户

发布于 2011-03-01 13:41:01

问题出在参数"about"=>{"about"=>{"sexuality"...中的额外about

您的表单中可能包含类似以下内容:

<%= form_for @about do |f| %>
  <%= f.fields_for @about do |a| %>
     <%= a.text_field :sexuality %>
     ...
  <% end %>
<% end %>

fields_for块会在参数中产生额外的about。只要去掉它,你就应该可以开始工作了。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5150683

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档