I have this simple case statement :
class_name = case link_path
when current_page?(jobs_path) then 'current'
when current_page?(open_estimates_path) then 'current'
when current_page?(tasks_path) then 'current'
when current_page?(calendar_dispatch_path) then 'current'
end
debugger
At my break point, I can ask, current_page?(tasks_path), and it will return true.
Then I ask class_name, and it returns nil.
What syntactical error am I making here?
ruby 1.9.2p136
Rails 3.0.10
The
casestatement doesn’t do what you think it does. It compares the subject to all the values stated in thewhenbranches and returns the value of the first matching one. For example:You are giving it
link_pathas the subject, so it compareslink_pathto each of the booleans in yourwhenbranches. Aslink_pathis probably not a boolean value, none of the branches will be evaluated. Maybe you want something like this instead:This will set
class_nameto"current"iflink_pathis the current page, otherwise to"".