I want to explicitly pass a boolean false to params[:closed] and I've got something like this:
我想显式地将一个布尔值false传递给params [:closed],我有这样的东西:
= 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?
但这不起作用,因为rails正在处理一个错误的布尔值,因为我不想为这个参数设置任何东西,有没有办法解决这个问题?
update: the false I'm passing in is a boolean and I want an url like this
更新:我传入的错误是一个布尔值,我想要一个像这样的URL
\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?
附带问题,将布尔作为params传递是一种好习惯吗?
3 个解决方案
#1
Probably too late now, but try using a string instead:
现在可能太晚了,但尝试使用字符串代替:
appointments_path(:closed => 'false')
#2
That will result in ?closed=false. params[:closed]
will then return "false"
, which is a string, and will evaluate to true.
这将导致?closed = false。 params [:closed]将返回“false”,这是一个字符串,并将评估为true。
I would have used named routes for this, instead of manic if/else checking in the 'index' action.
我会使用命名路由,而不是manic if / else检查'index'动作。
# routes.rb
map.resources :appointments, :collection => {:closed => :get, :open => :get}
# controller
def closed
@appointments = Appointment.closed
end
def open
@appointments = Appointment.open
end
#3
It sounds like you need 3 different options. Closed not selected, closed set to true or set to false.
听起来你需要3种不同的选择。未选中已关闭,已关闭设置为true或设置为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.
如果您需要区分已关闭和未被选中并将其设置为false,则需要使用字符串和参数[:closed] =='false'。如果rails有一些将url字符串转换为布尔值的catch,你将无法将字符串“false”作为一个奇怪的参数传递。
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:
您可以在_path方法中使用:closed => true或:closed => false指定它,然后使用params [:closed]进行测试,如下所示:
if params[:closed]
blah
else
other_blah
end
#1
Probably too late now, but try using a string instead:
现在可能太晚了,但尝试使用字符串代替:
appointments_path(:closed => 'false')
#2
That will result in ?closed=false. params[:closed]
will then return "false"
, which is a string, and will evaluate to true.
这将导致?closed = false。 params [:closed]将返回“false”,这是一个字符串,并将评估为true。
I would have used named routes for this, instead of manic if/else checking in the 'index' action.
我会使用命名路由,而不是manic if / else检查'index'动作。
# routes.rb
map.resources :appointments, :collection => {:closed => :get, :open => :get}
# controller
def closed
@appointments = Appointment.closed
end
def open
@appointments = Appointment.open
end
#3
It sounds like you need 3 different options. Closed not selected, closed set to true or set to false.
听起来你需要3种不同的选择。未选中已关闭,已关闭设置为true或设置为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.
如果您需要区分已关闭和未被选中并将其设置为false,则需要使用字符串和参数[:closed] =='false'。如果rails有一些将url字符串转换为布尔值的catch,你将无法将字符串“false”作为一个奇怪的参数传递。
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:
您可以在_path方法中使用:closed => true或:closed => false指定它,然后使用params [:closed]进行测试,如下所示:
if params[:closed]
blah
else
other_blah
end