dorofr
dorofr

Reputation: 25

Rails controllers names

I'm new in Ruby on Rails world. I want to use singular controller name instead of plural.

Here is the url that I want to achieve.

http://foobar.com/admin/login

Here is what I try

rails g controller admin::login

When I try to access

http://foobar.com/admin/login

I get

uninitialized constant Admin::LoginsController

How to create singular controller instead of plural?

Upvotes: 1

Views: 246

Answers (2)

tiktak
tiktak

Reputation: 1811

You should look at the rails namespaces first. For example:

namespace :admin do
  resources :posts, :comments
  match 'login' => 'controller#action'
end

It will produce routes with admin prefix. So if you want singular controller you should dive deeper into inflectors. Check following links:

For example, add following lines to your config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "login"
end

In general you should user user or session controller for login action, of course. Look at, for example, devise gem for more information and usage.

Upvotes: 1

Justin
Justin

Reputation: 1956

Whenever you go against Rails convention, the first thing you have to ask yourself is why are you doing it that way? If you are creating a login function, you should have an administrators/users controller and a sessions controller. You can still create the url /admin/login/ by using nested resources and custom routes, but have the logic in the sessions controller.

But if you really need to change that for some reason, you can do it using this type of technique: override default pluralize for model-name in rails3.

Upvotes: 1

Related Questions