【发布时间】:2011-03-27 22:18:10
【问题描述】:
我的用户 new 表单中有这个用于单选按钮的 ruby 代码:
<%= f.fields_for :profile, Profile.new do |t| %>
<div class ="field">
<%= t.label :type, "Are you an artist or listener?" %><br />
<p> Artist: <%= t.radio_button :type, "artist" %></p>
<p> Listener: <%= t.radio_button :type, "listener" %></p>
</div>
<% end %>
我想设置我的 Profile 模型的 type 属性。但是,类型未设置,默认为nil。我尝试在我的配置文件控制器中创建此 create 方法,但它不起作用:
def create
@profile = Profile.find(params[:id])
if params[:profile_attributes][:type] == "artist"
@profile.type = "artist"
elsif params[:profile_attributes][:type] == "listener"
@profile.type = "listener"
end
end
如何将type 正确设置为“艺术家”或“听众”?
更新:
我收到此错误:WARNING: Can't mass-assign protected attributes: type
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3