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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3.2 – 无法批量分配受保护的属性:配置文件,前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我阅读了很多相关的帖子,但找不到为什么它不适合我.我还有一个“无法批量分配受保护的属性配置文件”……
我究竟做错了什么 ?

我有一个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
原文链接:https://www.f2er.com/ruby/264589.html

猜你在找的Ruby相关文章