Rails: allow singular namespace and resource to share a view folder - ruby-on-rails

I have the following routes defining certain resources:
resource :account, :only => [:show]
namespace :account do
resource :billing
end
So, I have an AccountsController which generates the "show" page at /account.
I also have a BillingsController which I want to be viewed at /account/billing.
This is working fine, but one thing that's bugging me is the convention says the view folder for the AccountsController is plural even though its a singular resource -- not a big deal, but when creating a matching namespace for the nested resource I now have two seperate view folders -- /app/views/account(for namespace) and app/views/accounts(for account resource).
So, this kind of throws me off.
What would be the best way to make the AccountsController use the singular account folder for views?

For what it's worth, I decided it would be easiest to change AccountsController to AccountController and change my routes.rb file like so:
resource :account, :controller => 'account'
That was a little cleaner than anything I could figure out.

There are a bunch of options, but using self.prepend_view_path("views/account") in a method called by a before_filter in the AccountsController should work. It will force it to look in the specified directory for a view, before checking the default.

Related

Adding Rails Individual Route

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

Rails: Canned/RESTful way for accessing data returned by a method of a model

In my app I have a User model which defines a history method that returns a list of Activity objects, showing the last N actions the user has carried out. The UserController#history method wires this with a view.
The code looks as follows:
class UserController < ApplicationController
def history
user = User.find(params[:id])
#history = user.history(20)
end
end
class User < ActiveRecord::Base
has_many :activities
def history(limit)
...
end
end
Naturally, I also added this line to my routes.rb file:
match '/user/:id/:action', :controller => 'user'
so now when I go to localhost:3000/user/8/history I see the history of user 8. Everything works fine.
Being a Rails NOOB I was wondering whether there is some canned solution for this situation which can simplify the code. I mean, if /user/8 is the RESTful way for accessing the page of User 8, is it possible to tell Rails that /user/8/history should show the data returned by invoking history() on User 8?
First of all the convention to name controllers is in the plural form unless it is only for a single resource, for example a session.
About the routes I believe you used the resources "helper" in your routes, what you can do is specify that the resource routes to users also has a member action to get the history like this
resources :users do
member do
get :history
end
end
I think there is no cleaner way to do this
You can check it here http://guides.rubyonrails.org/routing.html#adding-more-restful-actions
As far as the rails standards are concerned, it is the correct way to show the history in your case. In rails controllers are suppose to be middle-ware of views and model, so defining an action history seems good to me.
And you can specify the routes in better way as:
resources :user do
get 'history', :on => :member #it will generate users/:id/history as url.
end

Using "resources" without scaffold in rails

I do not want to use scaffold in my rails app and there is something I do not quite get (yet!) regarding the "resources" keyword in the routes.rb.
Is the usage of "resources" linked to the generation with "scaffold" ?
My understanding is that "scaffold" will create a bunch of files, and among them a controller with the correct action's names (index, show, ...).
If I create the model (without using "scaffold") and then create a controller with the correct actions and the correct views, would this be sufficient to use "resources" in the routes.rb or will I miss something ?
Scaffold and resources is not linked in any way.
It's just that resources is already a kind of scaffold in that it always creates the CRUD routes that are also generated by the scaffold.
So if you write:
resources :users
You end up creating 6 routes for:
index
new
create
edit
update
destroy
You can limit what resources are generated by using :only
resources :users, :only => [:index, new]
Where only the index and new routes will be created.
You then can create those actions in your controller and add the appropriate views for them.
In short: If you just put resources :users in your routes.rb you can create these actions yourself in the controller and it will just work. No need to create a scaffold for it.

Retrieve the controller name from a class name following RoR conventions

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.

Is it the rails way to use singular or plural controller names?

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

Resources