I want to add a conditional operator to the hash.

Asked 2 years ago, Updated 2 years ago, 43 views

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.

ruby-on-rails ruby

2022-09-30 20:22

2 Answers

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.


2022-09-30 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.