如何做一件事:
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:age].blank? or "20"
上面没有工作。如果 no param 可用于该属性,我希望能够设置默认值。
任何聪明的方法来执行此操作!
编辑1: 完整形式:
= simple_form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:people][:age_from] || "20"
= f.submit "Go »"
答案 0 :(得分:2)
您正在使用从您正在构建表单的对象中获取值的帮助程序。
因此,在您的控制器中,您应该预设对象上的值。
def some_action
@people = People.new
@people.age = params[:age] || 20
end
然后在表单中,删除:selected
选项,它应该没问题。确保为@people
构建表单:
= simple_form_for @people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age'
= f.submit "Go »"