How can I get current_page? to match multiple actions?
Asked Answered
C

4

16

My problem is that I'm trying to do something like

current_page?(controller: 'tigers', action:('index'||'new'||'edit'))

which returns true when the controller is tigers and the action is either index, new, or edit.

The above does not throw an error, but only matches the first action.

Help!

Chore answered 1/2, 2014 at 0:54 Comment(3)
Personally I would just write a method to check these one by one and be done with it. Jumping through hoops to try to one-line this check is not worth the time.Comment
Interesting. I'm still relatively noobish, trying to figure out the optimal way to do things. Appreciate the input.Chore
ruby is great and the magic methods it sometimes gives can be intoxicating at times, but never forget that sometimes just writing the code is the best way to solve the problem. ;)Comment
L
19

More verbose, but works:

current_page?(controller:'bikes') &&
 (current_page?(action:'index') ||
  current_page?(action:'new')   ||
  current_page?(action:'edit'))

Less verbose, also works:

params[:controller] == 'bikes' && %w(index new edit).include?(params[:action])

%w() is a shortcut for building arrays of space delimited strings

Littlejohn answered 3/2, 2014 at 19:52 Comment(2)
All good answers, I chose this one because its a one-liner like I was looking for. Thanks!Chore
every page visit is always including controller and action in its params therefore you could do the onelineSoever
H
6

You can also do something like this:

if current_page?(root_path) || current_page?(about_path) || current_page?(contact_path) || current_page?(faq_path) || current_page?(privacy_path)

Note: Do not leave blank space before parenthesis otherwise it won't work.

Heater answered 30/9, 2015 at 2:7 Comment(1)
Using the route helper as seen above seems more flexible than other options listed. I was running into an issue where a template in my root project using the more explicit syntax (e.g. supplying action, controller in a hash) was causing issues in a mounted engine that was sharing the same template. Basically, the engine was expecting the specified controller to be a part of the engine. Using the route path helper remedied the situation! Thank you.Fellner
A
2

An alternative to using the current_page? method is to check the params hash directly:

params[:action] == ('index' || 'new' || 'edit')

Will return true if on index, new, or edit. You can also access the controller through params[:controller].

Archaism answered 1/2, 2014 at 1:18 Comment(3)
This didn't seem to be working for me, but I may have been putting it in the wrong place.Chore
It won't work. ('index' || 'new' || 'edit') basically equals 'index', because it's truthy.Athome
You could instead do: [:index, :new, :edit].include? params[:action]Peroxy
I
0

If you want all actions related to a specific controller you can use 'controller_name' method like this

if controller_name == 'your_controller_name'

'controller_name' is a method inherited from the ActionController::Base class, it's available in all views and helpers by default.

Immature answered 16/3, 2023 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.