views:

352

answers:

1

Okay, these two related questions are in reference to Railscast #21:

I'm having some trouble with routes. Two issues:

1) The routes in the tutorial seem to be relative to the root of the application; I want them to be relative to the root of the model. So

"http://example.com/login" I need to be "http://example.com/model/login" (and vice versa for logout).

I'm using permalinks to refer to my records, and I don't know how to specify an override, because every time I try to use "http://example.com/model/login" I get an error that says it can't find the record "login". How can I override this for login/logout?

2) Going to a custom route for me doesn't seem to keep the custom route in my address bar. So going to "http://example.com/login" gets me to the right page, but the browser now says "http://example.com/session/new" in the address bar. In the tutorial this doesn't happen: the app serves the correct page and keeps the custom route in the address bar. How can I get this to happen for me?

## Sessions Controller
class SessionController < ApplicationController
  def create
    session[:password] = params[:password]
    flash[:notice] = "Successfully Logged In"
    redirect_to :controller => 'brokers', :action => 'index'
  end

  def destroy
    reset_session
    flash[:notice] = "Successfully Logged Out"
    redirect_to login_path
  end
end

## Routes
ActionController::Routing::Routes.draw do |map|
  map.resources :brokers, :session
  map.login 'login', :controller => 'session', :action => 'create'
  map.logout 'logout', :controller => 'session', :action => 'destroy'
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
end
A: 

What do you mean with "root of the model"? You should make routes to controllers and their actions. The controller should communicate with the model.

The error messages can be a bit confusing, but as far as I can see, this url:

http://example.com/model/login

would call the action called login in the controller called model with an empty id (wich probably doesn't exist), using this route:

map.connect ':controller/:action/:id'

If you want to have a "subfolder" in your route you can use namespaces, I don't remember every detail about it but you can find a lot of info about it here. A word of warning: namespaces makes the route debugging a lot harder, I've had a lot of fun figuring out wich route is really used. I ended up creating many very specific routes to be sure the correct one was used.

Stein G. Strindhaug