如何从控制器中的 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
当我单击提交按钮时,值不会获取。请帮忙
<%= f.select :repetitive_task,options_for_select(["Every Day","Every Week", "Every Month"], params[:micropost][:repetitive_task])
, {:prompt => 'None'}, {:id => "select1"} %>
在options_for_select中传递第二个参数以显示所选值。
还可以使用 params[:micropost][:repetitive_task] 而不是在控制器中使用 params[:repetitive_task]。
如果你使用form_for助手(例如form_for:micropost),你应该在params[:micropost]中拥有所有值。在您的情况下,您应该有 :repetitive_task
params[:micropost][:repetitive_task]