4

I am using Ruby on Rails 3.0.7 and I would like to retrieve a controller name given a class name. That is, I have

Articles::Category

and I would like to retrieve

articles/categories

I would like to retrieve the controller name inside a view file not related to the Articles::Categories controller.

How can I do that (possibly using some Ruby on Rails core method)?

5 Answers 5

12
Articles::Category.name.underscore.pluralize
=> "articles/categories"

As mentioned by many others, there is no special method for that. But as long as you followed Rails conventions, this approach will take you far.

4

To get the controller name, say inside your view, you can do :

<%= controller.controller_name %>

To get the name of a class, say User, if you have a user object named user :

user.class.to_s 

Other than that, i don't think there's a correlation between a controller and a model, that can give you the controller name from a class name, because they are different things. You can maybe create a hash like {:controller => 'class_name'} and map those yourself.

5
  • 2
    I fear it doesn't answer the question Commented Jul 10, 2011 at 18:46
  • 1
    But, i answer in the last paragraph apneadiving :) I say that, to my knowledge, you cannot do that.
    – Spyros
    Commented Jul 10, 2011 at 18:53
  • Backo, in your update, you say that you would like to receive the controller name inside a view file. You can do that with the first code snippet in my answer.
    – Spyros
    Commented Jul 10, 2011 at 18:55
  • @SpyrosP - Calling '<%= controller.controller_name %>' method inside a view file not related to the 'Articles::Categories' controller I will get the controller name of the current controller (that is, not the 'Articles::Categories' controller).
    – Backo
    Commented Jul 10, 2011 at 18:59
  • But views are tied to controllers. If you have, say, a user controller with a user view, you do not invoke that view from another controller, unless it's a partial, in which case, you would still get the current controller.
    – Spyros
    Commented Jul 10, 2011 at 19:03
1
Articles::Categories.name.underscore #=> articles/categories

And it underscores camelcased words to so that:

RailsAdmin::ApplicationHelper.name.underscore #=> rails_admin/application_helper
2
  • As long as you haven't deviated from the naming conventions Rails relies on, this seems like a reasonable approach.
    – user483040
    Commented Jul 10, 2011 at 20:59
  • @gunn @jaydel - I was wrong. I was referring to 'Articles::Category' class name and not to 'Articles::Categories' class name! I updated the question as well.
    – Backo
    Commented Jul 11, 2011 at 6:53
0

Assuming that you really meant Articles::CategoriesController then Articles::CategoriesController.controller_path will give you what you want.

Update The question is, given the name of a Rails Model, how do you get the name of the associated controller?

The answer to that question is, you can't. There is not a one-to-one mapping from the model name to the controller name. The User model could be controlled by the users_controller.rb and/or admin/users_controller.rb or not have an associated controller at all. You can certainly guess at some likely possibilities based on Rails conventions but you can't know.

3
  • 1
    @ - I get this error: NoMethodError 'undefined method `controller_path' for #<Class:0x00000106a3cf08>'.
    – Backo
    Commented Jul 11, 2011 at 6:51
  • There must be some mistake, controller_path is defined on all classes that inherit from ActionController::Base. Are you trying to get the controller_path for the controller associated with a model, i.e. something that inherits from ActiveRecord::Base?
    – Ray Baxter
    Commented Jul 11, 2011 at 7:56
  • 1
    I have the model name (Articles::Category) and I would like to retrieve the related controller "name" (articles/categories).
    – Backo
    Commented Jul 12, 2011 at 23:09
0

Instead of underscore.pluralize just use tableize:

ActiveRecord::Base.name.tableize
=> "active_record/bases"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.