How do I map the `/users/sign_up` path to `/users/new`? - ruby-on-rails

I am using Ruby on Rails 3.2.2 and I would like to "use" the /users/sign_up PATH instead of /users/new in order to register new users to my application.
How can I make that "a là Ruby on Rails Way"?
Note: at this time I am using the following code in the routes.rb file but I think it isn't the best way to accomplish that I am looking for:
resources :users, :except => [:new] do
collection do
get 'sign_up'
end
end
What would you advise?

This should work:
resources :users, :path_names => {:new => 'sign_up'}
Its in the docs here

You can do the following:
match '/users/sign_up' => 'users#new', :as => :signup
and make links with the signup_path helper method

Related

Abstracting rails route

I want to replace the normal /users/:id route that is created by the resources command, with a more abstract /profile route. It won't be possible to view other users profiles in my app, and therefor the current route is unnecessary specific.
I have tried to overwrite the route created by 'resources :users' with:
get '/profile', to: 'users#show'
and other variances and combinations, but can't seem to get it right. Either the controller can't find the user because of a missing id or it simply can't find the route.
Thanks for the help!
You can use this code in routes.rb file:
resources :users, :except => :show
collection do
get 'profile', :action => 'show'
end
end
It will generate url "/users/profile".
But, if u want to use only '/profile', then don't create route as collection inside users resources block.
resources :users, :except => :show
get 'profile' => "users#show", :as => :user_profile
It will redirect '/profile' to show action in users controller.
I suggest simply adding a users/me route pointing to the show action of your UsersController like so:
resources :users, only: [] do
collection do
get 'me', action: :show
end
end
You can also use the match keyword in routes.rb file.
match 'users/:id' => 'users#show', as: :user_profile, via: :get

Rails namespaces and routing

I need help. I want administration for my rails application. I tried to set the routes with namespaces, but namespaces require a resource, and resource must have id in get request.
Anybody know how to set up correctly? I using windows machine. Thanks.
My routes :
Web::Application.routes.draw do
namespace :admin do
resources :access # GET http://localhost/admin/access/login/login - stupid??
end
match ':controller(/:action(/:id))(.:format)'
end
Try to use resource :access instead of resources :access
namespace :admin do
resource :access
end
It will generate routes:
admin_access POST /admin/access(.:format) admin/access#create
new_admin_access GET /admin/access/new(.:format) admin/access#new
edit_admin_access GET /admin/access/edit(.:format) admin/access#edit
GET /admin/access(.:format) admin/access#show
PUT /admin/access(.:format) admin/access#update
DELETE /admin/access(.:format) admin/access#destroy
namespace :admin do
get "login" => "access#login", :as => :login # GET http://localhost/admin/login - admin_login_path
end
If you don't have a set of restful resources, but just want a set of different controller methods, here's one way you can do it:
scope '/admin' do
get '' => "admin#index", :as => 'admin_home'
get '/users' => 'admin#users', :as => 'admin_users'
get '/other_admin_task' => 'admin#other_admin_task', :as => 'other_admin_task'
end

Customizing User Show Route in Rails

I want, instead of /user/:id I want the default user route to be /user:created_at I was able to get /user:id to work (without the second /) however when I try to do :created_at I get an error.
Does anyone know how to fix this? Also, even though I have match 'users:id', :to => 'users#show', :as => :user, :via => :get /user/1 still is a valid link since I have resources :users in my config/routes.rb. Is there a way to remove the default /user/:id when rails compiles the resources :users?
If I understand it correctly you can either use to_param: http://apidock.com/rails/ActiveRecord/Base/to_param
so if you used in your user model
def to_param
"#{first_name}_#{last_name}"
end
then user_path(#user) would generate /users/planet_pluto for example
to prevent a route to be generated by map.resources simply use :except
map.resources :user, :except => [:show]
hope that helps

Difference between :as option in Rails 2 and Rails3 routing?

In Rails 2.X we have:
map.resources :posts, :controller => 'posts', :as => 'articles'
This essentially creates an alias for our posts routes. For example, this sends "domain.com/articles/" to the posts controller index action.
In Rails3, however, the :as option behaves differently. For example:
resources :posts, :controller => 'posts', :as => 'articles'
sets a named route rather than an alias, and going to "domain.com/articles/" gives an error:
No route matches {:controller=>"posts"}
How do I get the old (Rails 2) :as behavior using the new (Rails 3) api?
PS: Please don't tell me to simply rename my controller. That's not an option for me.
From some cursory reading of the RoR guide on routing, I think you might need to try:
resources :articles, :controller => "posts"
(http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use)
You might also need to add :as => "articles", but that named helper might already be set up since you are adding :articles resources.
You can accomplish this same behavior using the path option:
resources :posts, :path => '/articles/'
Now for example /posts/new becomes /articles/new.

hyphen resources in rails 3 routes

How is it possible to use hyphen in resources urls?
For example: /my-model/ or /my-model/1.
If I define route as resources :"my-model" I get syntax error because rails generates method def hash_for_my-models_url(options = nil).
I have found the solution:
resources "my-models", :as => :my_models, :controller => :my_models
UPDATE:
As Timo Saloranta said in comment it works without :controller => :my_models in latest Rails 3 versions.
You can use the :as option to configure resourceful routes with hyphenated URLs:
map.resources :my_model, :as => "my-model"
results in
my_model_index GET /my-model(.:format) {:action=>"index",
:controller=>"my_model"}
...etc...
Have you tried a custom route?
map.connect "/my-model/:id", :controller => 'my-model-controller', :action => 'read'
This would invoke the 'read' method of 'my-model-controller.rb'.

Resources