“无法批量分配受保护的属性";具有嵌套的受保护模型 [英] "Can't mass-assign protected attributes" with nested protected models

查看:102
本文介绍了“无法批量分配受保护的属性";具有嵌套的受保护模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使此嵌套模型正常工作.我已经尝试了所有形式的复数形式/单数形式,一并删除了attr_accessible,还有谁知道呢.

I'm having a time trying to get this nested model working. I've tried all manner of pluralization/singular, removing the attr_accessible altogether, and who knows what else.

restaurant.rb:

restaurant.rb:

# == RESTAURANT MODEL
#
# Table name: restaurants
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Restaurant < ActiveRecord::Base
  attr_accessible :name, :job_attributes

  has_many    :jobs
  has_many    :users, :through => :jobs
  has_many    :positions

  accepts_nested_attributes_for :jobs, :allow_destroy => true

  validates :name, presence: true

end

job.rb:

# == JOB MODEL
#
# Table name: jobs
#
#  id            :integer          not null, primary key
#  restaurant_id :integer
#  shortname     :string(255)
#  user_id       :integer
#  created_at    :datetime         not null
#  updated_at    :datetime         not null
#

class Job < ActiveRecord::Base
  attr_accessible :restaurant_id, :shortname, :user_id

  belongs_to    :user
  belongs_to    :restaurant
  has_many      :shifts


  validates :name, presence: false

end

restaurants_controller.rb:

restaurants_controller.rb:

class RestaurantsController < ApplicationController

  before_filter :logged_in, only:  [:new_restaurant]

  def new
    @restaurant = Restaurant.new
    @user = current_user
  end

  def create
    @restaurant = Restaurant.new(params[:restaurant])
    if @restaurant.save
      flash[:success] = "Restaurant created."  
      redirect_to welcome_path
    end
  end

end

new.html.erb:

new.html.erb:

<% provide(:title, 'Restaurant') %>

  <%= form_for @restaurant do |f| %>
        <%= render 'shared/error_messages' %>

        <%= f.label "Restaurant Name" %>
        <%= f.text_field :name %>

        <%= f.fields_for :job do |child_f| %>

              <%= child_f.label "Nickname" %>
              <%= child_f.text_field :shortname %>

        <% end %>

        <%= f.submit "Done", class: "btn btn-large btn-primary" %>

  <% end %>

输出参数:

{"utf8"=>"✓",
 "authenticity_token"=>"DjYvwkJeUhO06ds7bqshHsctS1M/Dth08rLlP2yQ7O0=",
 "restaurant"=>{"name"=>"The Pink Door",
 "job"=>{"shortname"=>"PD"}},
 "commit"=>"Done"}

我收到的错误是:

ActiveModel::MassAssignmentSecurity::Error in RestaurantsController#create

Cant mass-assign protected attributes: job
Rails.root: /home/johnnyfive/Dropbox/Projects/sa

Application Trace | Framework Trace | Full Trace
app/controllers/restaurants_controller.rb:11:in `new'
app/controllers/restaurants_controller.rb:11:in `create'

任何人都有 ANY 提示如何使它工作?谢谢!

Anyone have ANY clue how to get this to work? Thanks!

推荐答案

in restaurant.rb:

in restaurant.rb:

应该是 attr_accessible :name, :jobs_attributes 代替 attr_accessible :name, :job_attributes

it should be attr_accessible :name, :jobs_attributes instead of attr_accessible :name, :job_attributes

这篇关于“无法批量分配受保护的属性";具有嵌套的受保护模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆