Why Controller names are plural but application_controller is itself a singular.
I was just curious when i scaffold a person and rails generated a people_controller for it, I was impressed but then I saw application_controller and it is singular. Does it has an explanation or it is as it is ?
Rails is full of conventions and plural names for controllers is simply one of those conventions. It is so Rails can locate the appropriate controller when you put resources :books in your routes.rb
You can override the convention and specify the controller explicitly, resources :books, controller: "library". In this case, Rails will look for LibraryController.
With ApplicationController, you don't call it directly and Rails doesn't need to do this lookup and so it can be named whatever. You can rename it, for example.
Related
In my Rails 4 app, an admin can moderate/approve posts, user accounts, and photos. I already have controllers for each of these (PostsController, UsersController, PhotosController) that handle the basic CRUD operations initiated by the user.
Having the update method for each controller seems incorrect and ... dirty. So does creating a single ModerationController with non-RESTful methods for each of the models.
I think I need something like a ModeratePostsController for each of the models, but I'm not sure how that gets scoped (under /admin?) or nested in routes.rb and generated as a controller.
Thoughts?
I was pretty much there.
I ran rails g controller Admin::Posts to create /app/controllers/admin/posts_controller.rb and in the routes file I added:
namespace :admin do
resources :teams
end
I have two Review models. The first is namespaced as Membership::Review and the second is not namespaced, Review. When I make a call on the non-namespaced model, Rails tries to lookup the namespaced one, instead. For instance:
library.includes(:reviews)
Gets me this error:
Expected C:/sites/shelflives/app/models/membership/review.rb to define Review
In my Library model, the Review association is made properly:
has_one :review
Even if I explicitly specify the model, I get the same error:
has_one :review, :class_name => "Review"
Any idea what's going on?
This kind of errors occurs when you adding extra config.autoload_paths in the application.rb with sub-folders of models.
You don't need to do this. All models will load automaticaly through the namespaces. You just need to organize correct structure with sub-folders of namespaces.
If you using namespaces with models you can use generator like this:
rails g model membership/review
That will generate correct namespaced model and will save it to membership sub-folder .
I have a CRUD resource defined in my routes.rb file: resource :user.
I'm adding a new controller method for the user called search_places, which is performed on the user to find other users with the same places. I'm adding a route it.
Right now, I have:
post '/user/search_place', which isn't very DRY. I'm new to Rails and I was reading the Rails routing documentation and figured that I could possibly use
resource :user do
collection do
post 'search_place'
end
end
Is this considered good practice? I know this works (it passes my rspec route test), but is that how its best done?
Thank you,
When you add second don't need of first.
Add this:
resources :user do
collection do
post 'search_place'
end
end
Remove this:
resources :user
That makes DRY :)
Suggestion: Resources name should be defined in plural if u follow rails convention. (i.e) resources :users
I am using Ruby on Rails 3.0.9 and I would like to build a controller name from a class name as well as possible following RoR naming conventions. For example, if I have the Articles::Comment class I would like to retrieve the articles/comments string.
Maybe it exists a RoR method created by developers to handle internally these conventions, but I don't know that.
How can I retrieve the controller name as in the example above?
You're looking for the underscore method. More info here.
"Articles::Comment".underscore
Or, if you've got the controller class itself, it would be like this:
Articles::Comment.name.underscore
EDIT
As of routes, they are built one piece at a time, even when you namespace them. When you do something like this:
map.resources :articles do |articles|
articles.resources :comments
end
What rails is going to do is, first:
"articles". classify # which yields "Article" then rails is going to append "Controller" to it
Then it's going to get "comments" and do the same, but this one is going to be routed under "/articles". Rails does not namespace internal resources, so, the controller has to be CommentsController, not Articles::CommentsController.
Only then you clearly namespace something, Rails is going to namespace your classes:
map.namespace :admin do |admin|
admin.resources :articles # will require controller "Admin::ArticlesController"
end
I hope it's clearer now.
Should I use /article or /articles ?
You can use either. If you are defining your routes using resource(s) then it's best to use plural controller names, because that is the default:
resources :articles
resource :articles
But it is possible to specify other controller names as well:
resources :articles, :controller => 'article'
resource :article, :controller => 'article'
You can use either. However, It is better to use the plural.
A controller is a class that, most often, accesses more than one instance of a model.
Eg: For a model by the name Subject, a controller accesses lot of instances of Subject, viz., subjects. Therefore, we name SubjectsController instead of SubjectController.
Plural
For the name of the actual controller class, and file it lives in. e.g class ArticlesController... living in /app/controllers/articles_controller.rb