Rubytastic
Rubytastic

Reputation: 15491

Rails 3: Text_field_tag default value if params[:something].blank?

How would one do something like:

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

Above doesen't work. I want to be able to set a default value if there is no param available for that attribute.

Any smart way to do this thx!

EDIT 1: Full form:

  = 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 »"

Upvotes: 1

Views: 2910

Answers (1)

Anthony Alberto
Anthony Alberto

Reputation: 10395

You're using helpers that are taking their values from the object you're building the form on.

So in your controller, you should preset the values on the object.

def some_action
  @people = People.new
  @people.age = params[:age] || 20
end

Then in the form, remove the :selected option and it should be fine. Make sure you build the form for @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 »"

Upvotes: 2

Related Questions