does anyone know if there is a rails routes converter online? I was not able to find one. I am trying to convert this line:
map.add_payment_profile 'add_payment_profile/:id', :controller => 'payment_profile_controller', :action => 'add_payment_profile'
Thanks!! So if I understand correctly:
map.create_cim_payment_profile 'create_cim_payment_profile_user', :controller => 'authorize_net', :action => 'create_cim_payment_profile', :only => :post would be
match 'create_cim_payment_profile_user' => 'payment_profile#create_cim_payment_profile', :as => :create_cim_payment_profile
I believe what you're looking for is
match 'add_payment_profile/:id' => 'payment_profile#add_payment_profile',
:as => :add_payment_profile
Rails 3 has a new controller#action shorthand for mapping controller actions. You will also need to specify :as to created a named route.
may be this will work:
match 'add_payment_profile/:id',:controller => "payment_profile_controller",
:action => "add_payment_profile"
Related
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.
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.
Here are some routes I have in Rails 2 and want to upgrade to Rails 3:
map.callback "/auth/:provider/callback", :controller => "authorizations", :action => "create" #omniauth
map.failure "/auth/failure", :controller => "authorizations", :action => "failure" #omniauth
map.signup 'signup', :controller => 'users', :action => 'new'
map.signin 'signin', :controller => 'user_sessions', :action => 'new'
map.signout 'signout', :controller => 'user_sessions', :action => 'destroy'
match "/auth/:provider/callback" => "authorizations#create", :as => :callback
match "/auth/failure" => "authorizations#failure", :as => :failure
match "signup" => "users#new", :as => :signup
match "signin" => "user_sessions#new", :as => :signin
match "signout" => "user_sessions#destroy", :as => :signout
That should get you going.
I would definitely checkout the screencast that apneadiving mentioned as well as Rails' take on routes.
Take a look at the rails_upgrade plugin at https://github.com/rails/rails_upgrade and its rake rails:upgrade:routes.
script/plugin install git://github.com/rails/rails_upgrade.git
rake rails:upgrade:routes
This will take your current routes file and rewrites it using the Rails 3 syntax. Copy the console output and look for any potential optimizations after you've read through the documentation in some of the other answers.
This should answer and make you learn:
http://railscasts.com/episodes/203-routing-in-rails-3
You may also find lots of great information at the Rails Routing from the Outside In.
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!
How can I make Twitter-style routes with Rails3?
I've tried the following:
match ':username', :controller => "users", :action => "show"
match ':username/:controller(/:action(/:id))', :path_prefix => '/:username'
EDIT
After some more digging through the docs, I did this and it seems to work:
scope '/:username' do
resources :clubs
end
What is the "scope" method exactly and is there an automatic way of generating link_to URLs in my views?
The following matcher will match /dhh/update/1
match ':username/update/:id' => 'updates#show'
'update#show' is new in Rails 3 and is the short version of :controller => 'updates', :action => 'show'
Try clubs_path(:username => 'bob').