Rails:在创建期间尝试设置 STI 类型时无法批量分配给这些受保护的属性
我刚刚开始学习 Rails 和 ruby,所以如果这太愚蠢了,请耐心等待。
我的应用程序中有几种不同的 AppModule 类型,它们具有不同的行为但相似的数据,因此我使用单表继承来保存它们。
但是,当尝试允许用户在 app_modules/new.html.erb
中明确选择他们想要的类型时,我收到警告警告:无法批量分配这些受保护的属性:类型.这是相关代码:
<% form_for(@app_module) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :type %><br />
<%= f.select( :type, options_from_collection_for_select(AppModule.subclasses().map{ |c| c.name}, 'to_s', 'to_s')) %>
</p>
<%= f.submit 'Create' %>
<% end %>
我尝试在模型文件中显式设置 attr_accessible :type
但它不起作用
我使用的是 Rails 2.3.8 和 ruby 1.8.7。
非常感谢任何帮助,谢谢...
I have just started out learning rails and ruby, so please bear with me if this is too dumb.
There are several different AppModule types in my app, which have different behavior but similar data, so I save them using single table inheritance.
However when trying allow the user to explicitly select which type they want in app_modules/new.html.erb
I get the warning WARNING: Can't mass-assign these protected attributes: type
. Here is the relevant code:
<% form_for(@app_module) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :type %><br />
<%= f.select( :type, options_from_collection_for_select(AppModule.subclasses().map{ |c| c.name}, 'to_s', 'to_s')) %>
</p>
<%= f.submit 'Create' %>
<% end %>
I have tried explicity setting attr_accessible :type
in the model file but it didn't work
I am using rails 2.3.8 and ruby 1.8.7.
Any help greatly appreciated, thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其添加到您的基类中:
我推荐 Adam 的方法,除非您正在处理混合列表。
Add this to your base class:
I recommend Adam's method unless you are dealing with a mixed list.
我只需要在“config/application.rb”中评论以下行即可解决此问题。
I just need to comment following line in my 'config/application.rb' to solve this issue.
您不必手动设置 type 属性。请改用您创建的子类。
You shouldn't ever have to set the type attribute manually. Use the subclasses you created instead.