Ruby On Rails Routes
I can't figure out how to get the following routes. Here's an extract from my routes.rb file:
map.resources :treatmen开发者_StackOverflow中文版ts
map.root :controller => "home"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
map.connect ':action', :controller => 'home' # replaces the need to manually map pure actions to a default controller
map.resources :bookings
map.resource :dashboard
map.resource :home
Now I do realise that the ordering matters, but I can't seem to get them to work correctly.
What I want is so http://localhost:3000/bookings/new actually takes you to an action http://localhost:3000/bookings/signmeup if you're either not signed in, or haven't got a login. The problem is that if I change my routes around, when I attempt to create a new booking after I have logged in, then it doesn't POST the form submission and just takes me back to the view page. This is definitely because of the routes as if I rearrange map.resources :bookings to be before all of them, then it works.
Any ideas?
As a rule of thumb, you want your resource routes to come before the generic :controller/:action/:id
routes (in fact, I go so far as to delete the generic routes entirely), since routes that are defined first take precedence over the ones that are assigned later.
As for redirecting to /bookings/signmeup if the user is not logged in, you should handle that with a before_filter
:
class BookingsController < ApplicationController
before_filter :check_login
# ...
protected
# This is a GENERIC example; change to fit your authentication method
def check_login
unless user_is_logged_in
redirect_to ...
end
end
end
精彩评论