Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RubyonRails:Can';t分配受保护的属性_Ruby On Rails_Ruby On Rails 3_Mass Assignment - Fatal编程技术网

Ruby on rails RubyonRails:Can';t分配受保护的属性

Ruby on rails RubyonRails:Can';t分配受保护的属性,ruby-on-rails,ruby-on-rails-3,mass-assignment,Ruby On Rails,Ruby On Rails 3,Mass Assignment,搜索了所有其他相同的问题,没有任何结果。 我有一个用户,它可以创建曲目,但也可以学习其他曲目。 学习其他曲目将保存在track_users中 model User.rb: # has track as author! has_many :tracks # has track as student! has_many :tracks_users has_many :courses, through: :tracks_users, source: :track accepts_nested

搜索了所有其他相同的问题,没有任何结果。 我有一个用户,它可以创建曲目,但也可以学习其他曲目。 学习其他曲目将保存在track_users中

model User.rb:

# has track as author!
has_many :tracks     
# has track as student!
has_many :tracks_users
has_many :courses, through: :tracks_users, source: :track
accepts_nested_attributes_for :tracks_users
模型轨道.rb

# has coach!
belongs_to :user
# has students!
has_many :tracks_users
has_many :students, through: :tracks_users, source: :user
accepts_nested_attributes_for :tracks_users
和model TracksUser.rb

  attr_accessible :track_id, :user_id, :as => [:default, :admin]
  belongs_to :track
  belongs_to :user
  accepts_nested_attributes_for :track
在my tracks/_follow.html.erb中:

<%= form_for(current_user.courses.build(track_id: @track)) do |f| %>
    <div><%= f.hidden_field :track_id %></div>
    <%= f.submit "Take track", class: "btn btn-large btn-primary" %>
<% end %>

这会抛出“不能批量分配受保护的属性:track_id”。
我甚至将config.active\u record.whitelist\u attributes=false设置为不起作用。

有单独的配置选项,明确用于开发阶段- 在config/environments/development.rb中:

# Raise exception on mass assignment protection for Active Record models
config.active_record.mass_assignment_sanitizer = :strict
这个错误是通过超过那个注释而消失的

我在StackOverflow中没有看到任何地方提到过这种配置。
找到了,谢谢tutsplus。

在设置config.active\u record.whitelist\u attributes=false后是否重新启动了服务器?该表单用于
课程
,不是
user
@Albert:当然我重新启动了。@AbM:Courses是关系跟踪\u users的同义词。如果我将表单从用户更改为跟踪作为主导实体,同样的结果是: