Change default route name - ruby-on-rails-3

In my Rails 3.1 application i have a UsersController, which gives me /users URL.
But i need /u instead for all REST actions.
What is the best practice for that?

You need to define the "path", in your case, modify routes.rb to
resources :users, :path => 'u'

Related

deleting vs. destroying a session in rails

I don't understand the difference. I've been following Michael Hartl's Rails tutorial and in my routes file I have the following:
resources :sessions, only: [:new, :create, :destroy]
match '/signin', to: "sessions#new"
match '/signout', to: "sessions#destroy", via: :delete
the site works with and without the via: :delete
The via option constraints the route to that HTTP verb. In this case, probably, the request is being made using the DELETE verb, so constraining this or not doesn't make any difference.
Note that destroy is the name of the action, delete is referencing the HTTP verb.

Rails "automatic" routes don't work, must specify with get "controller/method" every time?

I did
rails generate controller home index
But it adds this line to my routes.rb
get "home/index"
I thought Rails could deduce controller/method from the URL automatically? Why do I need to specify every get/post page?
Here's my complete routes.rb file:
Callisto2::Application.routes.draw do
root :to => "home#index"
resources :assets
end
root "/" works fine. so does /assets/*.
What's the problem with /home/index? I get the error:
Routing Error
No route matches [GET] "/home/index"
Try running rake routes for more information on available routes.
rake routes (run as apache user) gives me the following output:
root / home#index
Thanks for any clarifications. Not sure what I'm missing.
Edit: I didn't make this clear: I manually removed get /home/index from routes.rb to keep that file clean.
Rails used to add the so called catch all route at the bottom of your routes file:
match ':controller(/:action(/:id(.:format)))'
There was nothing 'automatic' or magical about these urls, just that every rails app started out with this route in their routes.rb
This has fallen out of favour, at least partially because it makes everything accessible over get, whereas
resources :books
Adds each route with the appropriate http verb. Listing routes explicitly is also a lot less verbose than when rails started out.
If your controller is home and the action is index your path is just /home.
You can find more information here.

How to apply activeadmin authentication to external page?

I am using activeadmin gem which is going fine, now i want to add its authentication to some external pages or as MVC you say to some action. don't mix it with the actions which can be added from admin/users.rd files. these are those action that reside out side of activeadmin.
To use a Devise (which Active Admin uses for authentication) for a page in the same app but not an actual Active Admin page I did the following:
in my routes.rb I added a new route:
devise_scope :admin_user do
resources :products
end
then in my products_controller.rb i added a before_filter to restrict access:
class ProductsController < ApplicationController
before_filter :authenticate_admin_user!
Hope that helps!

Rails 3.1 force .html instead of no extension

One of my clients wants his new Rails application to look more like his traditional web site. He wants to know if I can force urls to have a file extension, preferably .html.
I don't want to hard-code the extension in routes.rb as
match ':controller/:action/:id.html'
(or similar) because the client also wants to have a respond_to-style JSON API which requires the use of .:format.
Can this be done?
Just as Mattias Wadman suggested, in config/application.rb add:
AppName::Application.default_url_options = { :format => "html" }
But also change config/routes.rb to:
root :to => 'pages#home', :defaults => { :format => "html" }
Im no Rails routing expert but I tried to force HTML format by changing the default URL options and at least the URL helpers seams to generate .html URLs now, it's a start.
config/application.rb (at the bottom)
AppName::Application.default_url_options = {:format => "html"}

Rails 3 and Devise - Sign out not working after converting app to Rails 3

My routes
devise_for :users
devise_for :admin_users, ActiveAdmin::Devise.config #I have also tried removing this for any conflicts
resources :users
The sign out link. Routes to /users/sign_out just fine
<%= link_to "Logout", destroy_user_session_path, :method => :delete %>
Trying to sign out, gives me the error:
Couldn't find User with id=sign_out
If I then remove the resource :users, I get:
The action 'sign_out' could not be found for UsersController
What's wrong? The exact same code worked with Rails 2.3.8 and the corresponding Devise version
Logging in etc. works fine.
My setup is:
Ruby 1.9.2
Rails 3.1.1.rc3
Devise 1.4.8
First of all, using the same path for UsersController and Devise isn't a great idea. I would suggest using a path like '/accounts' for Devise.
But this probably isn't the cause of your sign out problem, as devise_for :users comes before resources :users in routes.rb. What seems to be the cause is, unless there'a typo in the question, that there's no comma after destroy_user_session_path. :method => :delete will be interpreted as a parameter to destroy_user_session_path unless there's a comma.
Also, make sure you're including jquery and jquery_ujs in application.js, as these are required for :method => :delete to work.