views:

920

answers:

3

I want to explicitly pass a boolean false to params[:closed] and I've got something like this:

= link_to 'appointments', appointments_path(:closed => false)

But this is not working as rails is treating a false boolean as I don't want anything set for this params, is there any way to get around this?

update: the false I'm passing in is a boolean and I want an url like this

\appointments?closed=false

but all I get is just \appointments. Hope this is a bit more clear.

Side question, is it good practise to pass booleans as params?

A: 

That will result in ?closed=false. params[:closed] will then return "false", which is a string, and will evaluate to true.

I would have used named routes for this, instead of manic if/else checking in the 'index' action.

# routes.rb
map.resources :appointments, :collection => {:closed => :get, :open => :get}

# controller
def closed
  @appointments = Appointment.closed
end

def open
  @appointments = Appointment.open
end
August Lilleaas
The false I'm passing in is a boolean not a string so params[:closed] is emtpy. I actually the the url to result in ?closed=false but how? If I set it to closed=true it works but not false.
sai
What about trying the approach that i suggested instead?
August Lilleaas
In the appointment index action I'm filtering the collection based on various parameters being passed in, so gets returned in index is dynamic. In the view the user can select from drop down for status, date, close or not etc. So it's not ideal to create named routes for all of them.
sai
+2  A: 

Probably too late now, but try using a string instead:

appointments_path(:closed => 'false')
ry
A: 

It sounds like you need 3 different options. Closed not selected, closed set to true or set to false.

If you need to distinguish closed from not being selected and being set to false like this you'll need to just use strings and params[:closed] == 'false'. If rails had some catch that would translate a url string to a boolean you wouldn't be able to pass the string "false" as a parameter which would be weird.

If your closed parameter was ALWAYS going to be true or false (without a not selected option) then doing it like you're doing is fine.

You specify it in the _path method with :closed => true or :closed => false and then test it with params[:closed] as in:

if params[:closed]
  blah
else
  other_blah
end