"Start Date"%> -6ren">
gpt4 book ai didi

ruby-on-rails - 如何从 ruby​​ on rails Controller 中的 f.select 标签获取选定值

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:32 24 4
gpt4 key购买 nike

我如何从 Controller 中的 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>

这是我的 Controller --

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] 而不是在 Controller 中使用 params[:repetitive_task]。

关于ruby-on-rails - 如何从 ruby​​ on rails Controller 中的 f.select 标签获取选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14560154/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com