Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 400 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to name a route in rails

#1
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?
Reply

#2
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`.
Reply

#3
From the [routing guide][1]:

> ##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.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through