Rails 3 "No Route Matches" error With Custom Named Route - ruby-on-rails

I have this custom route in my routes.rb
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
resources :businesses
And I have constructed a link like this:
<%= link_to business.name, business_permalink_path %>
However, whenever I visit the page with that link, I get this error:
No route matches {:controller=>"businesses", :action=>"show"}
I tried inverting the route order:
resources :businesses
match '/businesses/:permalink', :to => 'businesses#show', :as => :business_permalink
This does not work. It works if I change the link to this:
The show action exists and is defined in the file controllers/businesses_controller.rb.I want to create a custom URL using my permalink.
I am new in Rails and I know I am just missing something. What am I missing?

Try this:
<%= link_to business.name, business_permalink_path(business.permalink) %>

Try this:
match '/businesses/:permalink' => 'businesses#show', :as => :business_permalink
More here: http://railscasts.com/episodes/203-routing-in-rails-3

Related

How to remove /posts/ from URL?

How can I also show a blog post without /posts/ in the URL?
http://www.anthonygalli.com/posts/i-walk-the-line
http://www.anthonygalli.com/posts/50-shades-of-galli
http://www.anthonygalli.com/posts/tips-for-regaining-momentum
Shortened Version (Goal of Question):
http://www.anthonygalli.com/i-walk-the-line
http://www.anthonygalli.com/50-shades-of-galli
http://www.anthonygalli.com/tips-for-regaining-momentum
I have post as a MVC.
routes.rb
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
Change your routes.rb to:
resources :posts, path: '/' do
collection do
get 'about'
get 'feed'
get 'subscribe'
end
end
For documentation on Rails routing check: http://guides.rubyonrails.org/routing.html
Your routes should be updated to look something like this:
resources :posts
get 'about' => 'posts#about'
get 'feed' => 'posts#feed'
get 'subscribe' => 'posts#subscribe'
get ':id' => 'posts#show', :as => :post_by_slug
That as option will be important for linking to your new route if you want to link to the /post/-less paths:
<%= link_to post.title, post_by_slug_path(post) %>
P. S. I believe that the new route will need to be listed last in your routes so that the routes before it can have precedence. (Otherwise, trying to visit /about would try to load a post with a slug named about instead of the posts#about action. You should try this and see if that's true.)
Resource generates the standard CRUD urls, so you have to specify the posts route specifically. Something like this
get '/desired-url' => 'posts#index'

Change url for a route in Rails

I have a search scope for my users with the following route:
resources :users do
collection do
get :search
end
end
This however generates /users/search as url. I would like to have /search as url. I tried the following:
get '/search', as: :search
get '/search' => 'users#search', as: :search
get :search, to: 'users#search', as: :search
They don't seem to work since I keep getting routing errors. What would be the correct way to write it?
This one should work (without the leading '/') :
resources :users
get 'search' => 'users#search', as: :search
The named helpers for this route will be search_path and search_url
you can also use match:
match "/search", to: "users#search", via: "get"

Rails 3 Routing: non-CRUD controller

I need a bit of help with converting routing from Rails 2 to Rails 3.
In app/views/layouts/application.html.erb, I have:
<%= link_to "Reports", reports_path %><br>
There is a ReportsController, and in app/views/reports/index.html.erb, I have this:
<%= link_to "Clients With Animals", :action => "getAnimals", :controller => "clients" %>
Then, in config/routes.rb, I have this (Rails 3)
match '/reports' => "reports#index"
match '/clients/getAnimals', to: "clients#getAnimals"
I get this error when I click on the "getAnimals" link on the reports page:
ActiveRecord::RecordNotFound in ClientsController#show
Couldn't find Client with id=getAnimals
I don't want "getAnimals" to be the ID - I want it to be the action, instead.
How do I do that?
Assuming you also have a resources :clients entry, you want to make sure match '/clients/getAnimals', to: "clients#getAnimals" is above it (Rails will match whatever it hits first).
However, the better way may be to put it in the resource:
resources :clients do
get 'getAnimals', :on => :collection
end

No route matches a named route

My route file is like this
match '^movies\?.*\&commit=Refresh$', :to =>'movies#filter', :via => :get
resources :movies
match 'movies/sort_by/:criteria', :to => 'movies#sort_by', :as => 'sort_by'
where I want to match the 1st route with
movies?utf8=✓&ratings[PG-13]=1&ratings[PG]=1&commit=Refresh
. And in the view, I define a submit_tag that will execute the 1st route: = submit_tag 'Refresh', filter_by_path(). But I keep getting:
No route matches {:controller=>"movies", :action=>"filter"}
I don't know how to solve this since I try to check my route on rubular.com and it matches the link perfectly.
Try using :as in the first route
match '^movies\?.*\&commit=Refresh$', :to =>'movies#filter', :as => 'filter_by'
Then in your view,
= submit_tag 'Refresh', filter_by_url

:as in rails routes.rb

In config/routes.rb, I tried both:
root :to => 'things#index', :as => 'things'
and
root :to => 'things#index'
When I hit http://localhost:3000/, both approaches work, and nothing seems to be different.
What is the :as option used for?
The :as option forms a named route.
Usually it's used in a non-root route. For example:
match '/search' => 'search#search', :as => 'search' # SearchController#search
You could then do something like:
<%= link_to search_path, 'Click Here to Search!' %>
search_path and search_url are defined because of the :as
For a root route, you don't really need :as because the the URL helpers root_path and root_url are defined for you by Rails.
Rails 4 compatible.
In path_to_your_app/config/routes.rb
get "/profile/edit" => "users#profile_edit", :as => "edit_me"
Since ruby 2.0 you can use:
get "/profile/edit", to: "users#profile_edit", as: "edit_me"
In path_to_your_app/app/views/**in required view
<%= link_to "Edit profile", edit_me_path %>
Do not use match if you aren't sure you need it:
It creates a vulnerability when you use it in next pattern:
match ':controller/:action/:id'
From documentation:
You should not use the match method in your router without
specifying an HTTP method. If you want to expose your action to both
GET and POST, add via: [:get, :post] option. If you want to expose
your action to GET, use get in the router:
Instead of: match "controller#action"
Do: get "controller#action"
Read more about:
About match
http://github.com/rails/rails/issues/5964
About routes mapping
http://apidock.com/rails/v4.0.2/ActionDispatch/Routing/Mapper/Base/match
http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper/Base.html
About routes in general
http://api.rubyonrails.org/classes/ActionDispatch/Routing.html
The :as option creates a named path. You can then call this path in your controllers and views (e.g. redirect_to things_path). This isn't very useful for the root path (as it already named root), but is very useful for new routes you add.

Resources