views:

167

answers:

2

Hi guys,

I'm trying out Rails, and I've stumbled across an issue with my routing.

I have a controller named "Account" (singular), which should handle various settings for the currently logged in user.

class AccountController < ApplicationController
    def index
    end

    def settings
    end

    def email_settings
    end
end

How would I set-up the routes for this in a proper manner? At the moment I have:

match 'account(/:action)', :to => 'account', :as => 'account'

This however does not automagically produce methods like account_settings_path but only account_path

Is there any better practice of doing this? Remember the Account controller doesn't represent a controller for an ActiveModel.

If this is in fact the best practice, how would I generate links in my views for the actions? url_to :controller => :account, :action => :email_settings ?

Thanks!

+2  A: 

To get named URLs to use in your views, you need to specify each route to be named in routes.rb.

match 'account', :to => 'account#index'
match 'account/settings', :to => 'account#settings'
match 'account/email_settings', :to => 'account#email_settings'

Or

scope :account, :path => 'account', :name_prefix => :account do
  match '', :to => :index, :as => :index
  match 'settings', :to => :settings
  match 'email_settings', :to => :email_settings
end

Either works the same, it's just a matter of choice. But I do think the first method is the cleanest even if it isn't as DRY.

Samuel
A: 

You could also define it as a collection on the resource:

  resources :login do
    collection { get :reminder, :test }
  end

Of course, this also defines the default CRUD actions as well. I'm currently only using two of those for my not-an-actual-model controller, but I don't think/expect there will be any problem with the extra routes.

Kevin
that add a lot of extra route useless
shingara