加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3.2 – 无法批量分配受保护的属性:配置文件,

发布时间:2020-12-16 23:31:35 所属栏目:百科 来源:网络整理
导读:我阅读了很多相关的帖子,但找不到为什么它不适合我.我还有一个“无法批量分配受保护的属性:配置文件”…… 我究竟做错了什么 ? 我有一个User模型和一个具有一对一关系的相关Profile模型. 这里的用户模型(简化) class User ActiveRecord::Base attr_accessi
我阅读了很多相关的帖子,但找不到为什么它不适合我.我还有一个“无法批量分配受保护的属性:配置文件”……
我究竟做错了什么 ?

我有一个User模型和一个具有一对一关系的相关Profile模型.
这里的用户模型(简化)

class User < ActiveRecord::Base
  attr_accessible :email,:password,:password_confirmation,:profile_attributes,:profile_id
  has_secure_password

  has_one :profile

  accepts_nested_attributes_for :profile
end

Profile模型

class Profile < ActiveRecord::Base
attr_accessible :bio,:dob,:firstname,:gender,:lastname,:user_id

belongs_to :user
end

我的用户控制器

def new
@user = User.new 
  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @user }
  end
end

def create
@user = User.new(params[:user])
@user.build_profile

respond_to do |format|
  if @user.save

    format.html { redirect_to @user,flash: {success: 'User was successfully created. Welcome !'} }
    format.json { render json: @user,status: :created,location: @user }
  else
    format.html { render action: "new" }
    format.json { render json: @user.errors,status: :unprocessable_entity }
  end
end
end

如果它可以提供任何帮助,用户和配置文件都是脚手架.

我也尝试使用’:profiles_attributes’而不是User attr_accessible中的’profile_attributes’,同样的问题……

在用户控制器中也试过’@ user.profiles.build’而不是’@ user.build_profile’……同样的结果……

任何有关解释的帮助都会很棒(我是铁道上的菜鸟,请原谅我)

编辑
我使用的简单形式

<%= simple_form_for(@user) do |f| %>
<%= f.error_notification %>


<%= f.simple_fields_for :profiles do |p| %>
    <div class="nested-form-inputs">
      <%= p.input :lastname %>
      <%= p.input :firstname %>
    </div>
<% end %>


<div class="form-inputs">
  <%= f.input :email %>
  <%= f.input :password %>
  <%= f.input :password_confirmation %>
</div>

<div class="form-actions">
  <%= f.button :submit %>
</div>

<% end %>

干杯

解决方法

您引用的错误消息表示无法批量分配受保护的属性:配置文件.我相信你需要一个attr_accessible:个人资料(或者可能:个人资料)

我有一个应用程序

accepts_nested_attributes_for :order_items
attr_accessible :order_item

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读