"No routes matches" when using current_page in Rails 3
Asked Answered
C

3

11

Has anybody experienced routes mysteriously becoming undetectable when using current_page? in Rails 3? Even with a fully generated scaffold complete with routes, a view, and a controller, I am getting a "No route matches" error.

Here's the code:

if current_page?(:controller => 'users', :action => "show")

If I add a "match" command to routes.rb, it works fine, but why would I need to do that if the resources have already been created? What am I missing?

Clyburn answered 11/4, 2011 at 21:47 Comment(0)
H
18

You're missing the id parameter from this helper:

current_page?(:controller => "users", :action => "show", :id => "1")

It expects you to pass a full route through. If you don't want this and only want to match on the controller and action then I would recommend coding your own.

Hoppe answered 11/4, 2011 at 21:49 Comment(1)
Is there any way to make it not worry about the :id parameter? E.g. can we use regex or something? Or is there another helper that can be used in place of current_page?Amargo
C
20

If you just want to test the current controller, you can do the following:

if params[:controller] == 'users'

Similarly, if you're using a namespaced controller, you can just use a slash to separate the namespace(s) from the controller name, e.g.:

if params[:controller] == 'advertising/users'
Coated answered 27/2, 2012 at 4:3 Comment(0)
H
18

You're missing the id parameter from this helper:

current_page?(:controller => "users", :action => "show", :id => "1")

It expects you to pass a full route through. If you don't want this and only want to match on the controller and action then I would recommend coding your own.

Hoppe answered 11/4, 2011 at 21:49 Comment(1)
Is there any way to make it not worry about the :id parameter? E.g. can we use regex or something? Or is there another helper that can be used in place of current_page?Amargo
N
0

Depending on your routes, to look for a generic show action without ID you could look for e.g. !current_page?(:controller => "users", :action => "index").

Nobles answered 8/2, 2021 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.