So Authlogic ships with some pretty confusingly (for an end user) named routes. For example, instead of /login/new, you get /user_session/new, and so on. Then, when a user can't login, the error message appears as "This user session could not be saved."
It's a small thing, but that's just kind of... ugly, to me. What's a graceful way to rename the default session routes to something more meaningful (and easier to type)?
BTW, we are totally invested in Authlogic, so replacing it is a no-go
Rails lets you rename routes to your hearts desire. Assuming you're using Rails 2.x, the following routes should do the trick:
map.login, 'login', :controller =>'user_sessions', :action => 'new'
map.logout, 'logout', :controller => 'user_sessions', :action => 'destroy'
map.signup, 'signup', :controller => 'users', :action => 'new'
You can call them by using the following:
login_path
logout_path
signup_path
Related
I have a login form (/user).
And methods:
-authenticate
-logout
And one view index.html.erb, it contains the login form, authenticate is work fine, but I think I have problems with routing.
Can you give me a sample route?
My router looks like:
map.logout 'logout', :controller => 'sessions', :action => 'destroy'
map.login 'login', :controller => 'sessions', :action => 'new'
In sessions control have destroy method, but when I type /logout it's say: Missing template session/destroy.erb in view path app/view
A sample route in Rails 3
match '/user(/index)' => 'users#index'
It would be helpful if you could clarify the issue you're having (and post relevant code).
All routing system is explain on guides :
http://guides.rubyonrails.org
This one in particular : http://guides.rubyonrails.org/routing.html
I have been using the following route successfully in my Rails 2.x application:
map.user ':id', :controller => 'users', :action => 'show'
This, as my lowest route, properly catches things like /tsmango and renders Users#show.
I'm now trying to add a second, similar route like:
map.post '~:id', :controller => 'posts', :action => 'show'
Because neither my users or my posts are allowed to contain ~ and because this route will appear above my map.user route, I assumed this would properly catch any call starting with /~ and render my Posts#show action. Unfortunately, I'm having trouble getting this one to work.
What's interesting is that this similar route works perfectly:
map.post ':id~', :controller => 'posts', :action => 'show'
Although, I'm certainly willing to go with ':id~' since it has the same result, at this point I'm really just frustrated and curious as to how you would build a route that matches '~:id'.
It's worth mentioning that I do not want to modify my to_param method or my actual user and post slugs to include the prepended ~. I just want that in a route to indicate which action should handle it. Unless I'm mistaken, this rules out the use of something like:
:requirements => {:id => /\~[a-zA-Z0-9]/}
Thanks, in advance, for any help you can provide!
Update: I'm aware of route priority and stated above that I am placing the '~:id' route above the ':id' route. I receive the following error while trying to generate the url like post_path(#post):
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym
Routes are prioritized depending of the order in which they're declared.
When you define first the :id route, the second one is never executed.
In order for this to work, you just have to first define the ~:id route and then the :id one.
map.post '~:id', :controller => 'posts', :action => 'show'
map.post ':id', :controller => 'users', :action => 'show'
I have a Reports controller and various reports:
http://localhost/reports/main/this_month
http://localhost/reports/main/last_month
http://localhost/reports/main/this_year
I wanted http://localhost to default to http://localhost/reports/main/this_month. That is easy enough using map.root in my routes.rb.
However when I do this any links to http://localhost/reports/main/this_month are now shortened to just http://localhost. I want the links to stay full
I think it is very possible in Rails 2. The url string that is generated depends on which url helper you call in your view.
map.reports '/reports/:action/:timeframe', :controller => :reports
# todo pretty this up with some more named routes for reports
map.root :controller => "reports", :action => "main", :timeframe => "this_month"
Now, root_url will be http://locahost/. When you use reports_url(:action => 'main', :timeframe => 'this_month'), it will be http://localhost/reports/main/this_month. They both render the same action.
It sounds like you have set up the root, but just don't create any links with root_url.
One option is using a dummy controller that makes a redirect_to.
Routes:
map.reports '/reports/:action/:timeframe', :controller => :reports
# this triggers the action 'index' on 'welcome'
map.root :controller => "welcome"
And then on the Welcome controller:
class WelcomeController < Application: ApplicationController
def index
redirect_to :controller => "reports", :action => "main", :timeframe => "this_month"
end
end
As far as I know this is not possible in Rails 2 by default. There is a plugin called Redirect Routing that will allow it, however, which you could look into. In Rails 3, this functionality is built in. You can read about it in The Lowdown on Routes in Rails 3 over at Engine Yard.
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
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.
I am struggling with the route setup for a Rails application. I have installed restful_authentication and mostly followed the instructions. I have set up the routes this way:
map.login '/login', :controller => 'sessions', :action => 'new'
map.logout '/logout', :controller => 'sessions', :action => 'destroy'
map.resource :session
If you're not logged in, you're redirected to http://localhost:3000/session/new.
It makes some kind of sense, as the code in lib/authenticated_system.rb says redirect_to new_session_path.
But I thought the routes mapping was supposed to work both ways (code to URL and URL to code). Can someone explain? Thanks
map.resource :session creates a few named resources for you including new_session_path (see ActionController::Resources).
map.login and map.logout are just helper routes to make your code easier to understand. map.login (which generates login_path) points to the same controller/action combo as new_session_path does, it's just easier to remember at a glance what it does.