【发布时间】:2015-05-20 21:32:36
【问题描述】:
Ruby on Rails 4 中的强参数有问题。
我有 3 个模型 Entity、user、user_entity。
User_entity 将 entity 和 user 与 has_many :through 关联在一起。
这是完美运行的代码!
/views/user_entity/show.html.erb
<p>
<strong>User:</strong>
<%= @user_entity.user_id %>
</p>
当我修改它时,它不会!
<p>
<strong>User:</strong>
<%= @user_entity.user.name %>
</p>
我得到的错误是:
#已提取的未定义方法“用户” 来源(第 433 行附近):
别的 match = match_attribute_method?(method.to_s) 匹配 ? attribute_missing(match, *args, &block) : 超级 结尾 结束
我认为这是因为strong params 不允许我访问来自user 控制器的数据,但我不知道如何将这些数据列入user_entity 模型的白名单。
请帮忙。
/models/entity.rb
class Entity < ActiveRecord::Base
accepts_nested_attributes_for :user_entities
has_many :user_entities
has_many :users, :through => :user_entities
end
/models/user.rb
class User < ActiveRecord::Base
accepts_nested_attributes_for :user_entities
has_many :user_entities
has_many :entities, :through => :user_entities
end
/models/user_entity.rb
class User_entity < ActiveRecord::Base
belongs_to :entities
belongs_to :users
end
【问题讨论】:
-
您能否将您的
User、UserEntity和Entity模型添加到您的问题中? -
+ 你如何定义@user_entity?您在上面有一个拼写错误“eser_entity”,我猜它只是在这里,而不是在您的实际代码中?
-
错误输入:
@eser_entity而不是@user_entity
标签: ruby-on-rails ruby ruby-on-rails-4 associations strong-parameters