How to name a route in rails - ruby-on-rails

I have some routes looking like this :
match 'hotels/:action(/:id)', :controller => 'hotel', :action => /[a-z]+/i, :id => /[0-9]+/i
And i want to use something like hotels_dislike_path somewhere in my code which refers to /hotels/dislike
How can i do that?

From the routing guide:
3.6 Naming Routes
You can specify a name for any route using the :as option.
match 'exit' => 'sessions#destroy', :as => :logout
So, in your case, that would be:
match 'hotels/:action(/:id)', :controller => 'hotel', :action => /[a-z]+/i, :id => /[0-9]+/i
match 'hotels/dislike(/:id)', :controller => 'hotel', :id => /[0-9]+/i, :as => :hotels_dislike
match 'hotels/like(/:id)', :controller => 'hotel', :id => /[0-9]+/i, :as => :hotels_like
I don't think there's a way to do this dynamically (so you have to define one route for each action, basically). However, you can just define a couple of routes (like above) for the most used actions, and just use hotels_path :action => :really_like for more uncommon actions.

A lot has changed in the world of Rails since 2011 - this is how you would accomplish the same goal in Rails 4.
resources :hotels do
member do
post 'dislike'
post 'like'
end
end
The resulting routes:
dislike_hotel POST /hotels/:id/dislike(.:format) hotels#dislike
like_hotel POST /hotels/:id/like(.:format) hotels#like
hotels GET /hotels(.:format) hotels#index
POST /hotels(.:format) hotels#create
new_hotel GET /hotels/new(.:format) hotels#new
edit_hotel GET /hotels/:id/edit(.:format) hotels#edit
hotel GET /hotels/:id(.:format) hotels#show
PATCH /hotels/:id(.:format) hotels#update
PUT /hotels/:id(.:format) hotels#update
DELETE /hotels/:id(.:format) hotels#destro
Notice thats rails prefixes instead of postfixes the action - dislike_hotel_path not hotels_dislike.

Related

How can I redirect to a specific controller/action with similar routes?

My routes are redirecting to the same controller even when I specified different properties inside my routes.rb file.
These are my routes.
match ':clube_id' => 'clubes#show', :as => 'clean_cluble', via: [:get]
match ':project_id' => 'projects#show', :as => 'clean_project',via: [:get]
These are the links that I am using.
=link_to 'Project', :controller => "projects", :action => "show", :project_id=>'xxxxx'
=link_to 'Clube', :controller => "clubes", :action => "show", :id=>'cccc'
The link for projects works well, but the linl for clubes is redirecting to projects controller. that is the problem.
The URLs that I spect are:
http://host_name/project_name
http://host_name/clube_name
You didn't specify different properties, both routes look's identical for Rails. The match method expect any string(or id) in the ':clube_id' or ':project_id', for example:
host_name/soho_project or host_name/1
How is Rails can recognize for a which model it's related? It can be a Project or Club. I suggest add something like the anchor to a match method.
match 'club/:clube_id' => 'clubes#show', :as => 'clean_cluble', via: [:get]
match 'project/:project_id' => 'projects#show', :as => 'clean_project',via: [:get]
and helpers:
= link_to 'Project', clean_project_path(:project_id=>'xxxxx')
= link_to 'Clube', clean_cluble_path(:clube_id=>'cccc')
Read more about routes from the Rails guides.

How do I stop routes for user profile conflicting with other routes in ruby on rails?

How would I stop these 2 routes from clashing:
match "users/:id/edit", :to => "users#edit", :via => :get, :as => :settings
match '/:username', :controller => 'users', :action => 'show'
When I visit localhost:3000/settings it tries to find a user in my users table with the username "settings".
What I plan on doing is having a page settings then have things like settings/privacy, settings/general and also edit_profile
How can I have this but also still get to user localhost:3000/username
It must be possible as I've seen many ROR sites doing this.
Kind Regards
The settings route you have here is pointing to the users edit path.
Using settings_path will direct to users/:id/edit
What you want is:
match "/settings", :to => "users#settings"
match "/settings/:category", :to => "users#settings"
match '/:username', :controller => 'users', :action => 'show'
Now '/settings' will direct you to the settings method. The second additional route will handle the specific setting to route to (ie /settings/privacy) which will allow you to evaluate what to do in the view based on the params[:category] parameter available in your settings action. Alternatively this will give a privacy settings path:
match "/settings", :to => "users#settings"
match "/settings/privacy", :to => "users#settings", :as => :privacy_settings
match '/:username', :controller => 'users', :action => 'show'
No need for the :as statement, settings_path will still work. Then in the settings method of your UsersController you can use session information(current_user) to render the proper user.
The RESTful edit path will be handled and available through this statement in your routes file:
resources :users
So 'users/:id/edit' will still work.

Rails: simple routing question about :as => :something

Using Rails 3.1 (not sure if this is applicable to 3.0, etc.)
In routes.rb what's the difference between:
match "team" => "users#index"
and
match "team" => "users#index", :as => :team
I ask because docs say:
3.6 Naming Routes
You can specify a name for any route using the :as option.
match 'exit' => 'sessions#destroy', :as => :logout
This will create logout_path and logout_url as named helpers in your application. Calling logout_path will return /exit
But, in first example above I have access to team_path & team_url in my views?!? So what's the :as => :team do exactly? I must be overlooking something as I've seen example code written like:
match "logout" => "sessions#destroy", :as => :logout
match "login" => "sessions#new", :as => :login
match "signup" => "users#new", :as => :signup
though from my limited testing the :as => :something seems redundant?!?
It seems redundant but it's not... when the name of your route differ from the name you want to give.
The ActionDispatcher does a lot of things by default. You should try to trigger rake routes in your console to test this behavior.
Another example is the shortcut:
match "account/profile"
# same as
match "account/profile", :to => "account#profile"
which will create the named route: account_profile

Converting routes for rails 3

This is a pretty silly question but I'm having some real trouble figuring out. I want to convert the following routes to make it compliant for Rails 3 (from 2.8.x):
map.with_options :controller => 'static_pages', :action => 'show' do |static_page|
static_page.faq 'faq', :id => 'faq'
static_page.about 'about', :id => 'about'
static_page.blog 'blog', :id => 'blog'
static_page.support 'support', :id => 'support'
static_page.privacy 'privacy', :id => 'privacy'
static_page.howitworks 'howitworks', :id => 'howitworks'
static_page.contact 'contact', :id => 'contact'
static_page.terms_and_conditions 'terms_and_conditions', :id => 'terms_and_conditions'
end
Any help would be much appreciated!
I think I would do it like this:
scope '/static_pages', :name_prefix => 'static_page', :to => 'static_pages#show' do
for page in %w{ faq about blog support privacy howitworks contact terms_and_conditions }
match page, :id => page
end
end
This is awesome, I just wrote an article about this a couple weeks ago:
Routing in Ruby on Rails 3
It goes over most aspects of the conversion, with a downloadable sample app. While I didn't cover the with_options conversion specifically, I can do a little of that here. Here's a short way:
scope :static_pages, :name_prefix => "static_page" do
match "/:action", :as => "action"
end
This matches all the routes you have above, and your named routes would look like this:
static_page_path(:faq)
static_page_path(:about)
...and so on. If you want your named routes to still look like static_page_faq_path then you can specify them one at at time, like so:
scope '/static_pages', :name_prefix => 'static_page' do
match '/faq', :to => 'static_pages#faq'
match '/about', :to => 'static_pages#about'
# fill in all the rest here
end
I hope this helps!

get, match and resources in routes.rb

I am new to Rails. I found it very strange when I use the resources in routes.rb, after I redirect the page to controller/index, it render the controller/show .
I know GET controller/action is same as match "controller/action", :to => "controller/action"
I think the strange thing happens to me about the redirect, is similar to the GET and Match.
so I wonder what exactly the resources mean, can I use some simple match do the same thing?
resources is a shortcut for generating seven routes needed for a REST interface.
resources :widgets is equivalent to writing
get "widgets" => "widgets#index", :as => 'widgets'
get "widgets/:id" => "widgets#show", :as => 'widget'
get "widgets/new" => "widgets#new", :as => 'new_widget'
post "widgets" => "widgets#create", :as => 'widgets'
get "widgets/:id/edit" => "widgets#edit", :as => 'edit_widget'
patch "widgets/:id" => "widgets#update", :as => 'widget'
put "widgets/:id" => "widgets#update", :as => 'widget'
delete "widgets/:id" => "widgets#destroy", :as => 'widget'
it just saves you the trouble.
By the way, get is not exactly the same as match. get, post, put and delete are shortcuts for limiting the route to a single HTTP verb. The two route definitions below are equivalent.
match 'foo' => 'controller#action', :method => :get
get 'foo' => 'controller#action'

Resources