Currently, the code is as follows:
current_page?(controller: 'foo', action: 'bar')
This time, I would like to increase the action (even if it is a different action).
However, it doesn't work even if I write it with a conditional operator.
Even if I write as below, only one action works
-if current_page?(controller: 'locations', action: "new"||"index")
Please tell me how I can make it work well.
Thank you for your cooperation.
"new"||"index"
is always only "new"
, so I think the same reason why current_page?(controller: 'locations', action: "new")
doesn't work as intended.
If you look at the implementation of current_page?
it seems that it will be converted to url_for
in the middle, so it will be difficult to make a decision at once.
If you don't mind, I think it will work if you do the following.
case/when
to determine one by one-case
- when current_page?(controller: 'locations', action: "new")
- when current_page?(controller: 'locations', action: "index")
-if%w[new index].any?{|action|current_page?(controller: 'locations', action:action)}
Although I don't know how to use it or what the surrounding code is...Personally, For example, define what actions are targeted on the controller side, and It's like judging by the view. I think you can cut the constant definition and logic to current_page?
feels more complicated than I need, so controller_name
and action_name
simply Array#include?
and and so on.
class LocationsController<ApplicationController
HOGE_ACTIONS = %w [index new]
end
-if LocationsController::HOGE_ACTIONS.include?(action_name)
LocationsHelper
.
As a bonus, I'll put up another plan that's a little too much.
It's a wild move to change the behavior of current_page?
.
#helper
module ApplicationHelper
US>def current_page ?(options)
if options.kind_of?(Hash)&options[:actions]
options[:actions].any?{|action|super(options.merge(action:action)))}
else
super
end
end
end
# view
- if current_page?(controller: 'locations', actions: %w[new index])
Note that the argument passed to current_page?
is actions
instead of action
.
© 2024 OneMinuteCode. All rights reserved.