2

如何从控制器中的 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

单击提交按钮时未获取值。请帮忙

4

2 回答 2

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中传递第二个参数以显示所选值。

也使用 params[:micropost][:repetitive_task] 而不是在控制器中使用 params[:repetitive_task]。

于 2013-01-28T10:42:48.420 回答
2

如果您使用 form_for 助手(例如 form_for :micropost),您应该在 params[:micropost] 中包含所有值。在你的情况下,你应该有 :repetitive_task 在

params[:micropost][:repetitive_task]
于 2013-01-28T10:44:41.877 回答