Is the following naming convention in rails design?

My project name is clogged, so I called my models and controllers like this: Clog::User Clog::Userscontroller .

Is this a naming convention necessary?

+4
source share
2 answers

Yes, in accordance with the naming agreement, it helps a lot, because not only the rails use it to generate other names, but also other gems.

Depending on your question, you may ask if you need to specify the controller as a UserController, given that your model is called User. This is not necessary at all, and you can call it something else if it is better suited to your purpose.

In this case, you probably want to create several such controllers:

 My::AccountController # for eg. /my/account Admin::UsersController # for eg /admin/users/1 

For the user, you are referring to your own user account as "your account", so this makes sense. However, the prospect of an administrator is to manage user records. You can also call the controller one thing and serve it along a different route. In the routes file you can do this:

 namespace :admin do resources :users, :path => "user-accounts" end 

To repeat, your model name should not match the controller name. They are called similarly by association: UserController understands how to process user entries.

0
source

No, in a normal Rails project this is not necessary. Just name your models and controllers in the usual way, for example, User or UsersController.

Another thing is that when your project grows in size, you may need to organize your models in submodules. One way to do this is to extend your models with applications, for example, show here or.

Regarding the organization of controllers, one approach is to create a module in the lib directory, which you then include in your ApplicationController, for example:

In lib/authentication.rb :

 module Authentication def self.included(base) base.send :before_filter, :login_required base.send :helper_method, :current_user, :logged_in? end def current_user @current_user ||= User.find_by_remember_token(cookies[:remember_token]) if cookies[:remember_token].present? end #... end 

In app/controllers/application_controller.rb

 class ApplicationController < ActionController::Base include Authentication #... end 

For this you need to add

 config.autoload_paths << "#{config.root}/lib" 

to your config/application.rb file

However, if you plan to create a Rails project as a Rails Engine, you may want to follow a specific naming convention. A good example of a Rails Engine is forem .

+2
source

Source: https://habr.com/ru/post/1387073/


All Articles