如何从控制器中的f.select标签获取值。这是我的erb代码
<td>
<%= f.text_field :startdate , :id => "from", :placeholder => "Start Date"%>
</td>
<td>
<%= f.select :repetitive_task, options_for_select(["Every Day","Every Week", "Every Month"]), {:prompt => 'None'}, {:id => "select1"} %>
</td>
这是我的控制器 -
def create
@micropost = current_user.microposts.build(params[:micropost])
if params[:repetitive_task] == "Every Day"
@micropost.targetdate = params[:startdate]
elsif params[:repetitive_task] == "Every Week"
@micropost.targetdate = params[:startdate] + 7
elsif params[:repetitive_task] == "Every Month"
@micropost.targetdate = params[:startdate] + 30
else
end
if @micropost.save
flash[:success] = "Task created!"
redirect_to root_url
else
render 'static_pages/index'
end
单击“提交”按钮时,不会获取值。请帮忙
答案 0 :(得分:3)
<%= f.select :repetitive_task,options_for_select(["Every Day","Every Week", "Every Month"], params[:micropost][:repetitive_task])
, {:prompt => 'None'}, {:id => "select1"} %>
在 options_for_select 中传递 second 参数以显示所选值。
也可以使用params [:micropost] [:repetitive_task]而不是在控制器中使用params [:repetitive_task]。
答案 1 :(得分:1)
如果您使用的是form_for helper(例如form_for:micropost),则应该在params [:micropost]中包含所有值。在你的情况下你应该有:repetitive_task in
params[:micropost][:repetitive_task]