1

如何做这样的事情:

  = f.input_field :age,
                  :collection => 18..60,
                  :id => 'age',
                  :selected => params[:age].blank? or "20"

以上不起作用。如果该属性没有可用的参数,我希望能够设置默认值。

任何聪明的方法来做到这一点!

编辑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 »"
4

1 回答 1

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 »"
于 2012-09-17T12:40:32.747 回答