explicit get route is throwing an error - ruby-on-rails-3

I defined a custom route:
match 'folio/:id' => 'posts#show', :as => :folio, :via => :get
When I run rake routes command I have:
post GET /posts/:id(.:format) posts#show
folio GET /folio/:id(.:format) posts#show
And I put link for this element in my other page: link_to post.title, folio_path ,but when I enter it throws me an routing error:
No route matches {:controller=>"post", :action=>"show"}
Why it fails? When I'm using post#show in normal way it works like a charm, but with my custom route it fails - please help!

You need to specify the post so rails can fill the id part:
link_to post.title, folio_path(post)

Related

Rails 3 No route matches {:action=>"show", error

While there are similar questions on this error, most relate to how Rails route handle pluralisation, whereas this is different.
I have a link on the 'show' view page within a controller called MemberPage which I need to link to the 'new' action within a controller called Post
<%= link_to ((content_tag :i, "", :class => "icon-pencil") + content_tag(:span, 'create')), new_members_community_post_path %>
Within routes I have
resources :post
which produces the following related line in rake routes
new_members_community_post GET /members/community/post/new(.:format) members/community/post#new
Hovering over the link shows
127.0.0.1:3000/members/community/post/new
Clicking on it produces the error
No route matches {:action=>"show", :controller=>"members/community/member_page"}
Rails is matching both the wrong controller and the wrong action from that shown in rake routes
I've tried creating explicit match rules in routes.rb, eg (within the members/community namespace)
match '/new-post' => 'post#new'
and also replacing the link_to path with
:controller => :post, :action => :new
but cannot find anything that works

link_to redirect to a wrong controller

I have a layout for my admin area called 'layout_admin' . In 'layout_admin', I have :
<li><%= link_to 'Contenu', :action=>'index', :controller=>'contents' %></li>
<li><%= link_to 'Petitions', :controller => 'petitions', :action => 'index' %></li>
The first link for Contenu works fine, but the second one (for petitions) drives me to a strange error :
Routing Error
No route matches {:controller=>"admin/edito"}
In the address bar I have : localhost:3000/admin/petitions
In routes.rb I have :
namespace :admin do resources :petitions
end
I must also precise that "edito" is another controller outside the admin area which has one action "index". In routes.rb I have get "edito/index" concerning edito_controller.
Does somebody have an idea of the source of the problem ?
Thansk.
Full Rake routes :
temoignages GET /temoignages(.:format) temoignages#index
POST /temoignages(.:format) temoignages#create
new_temoignage GET /temoignages/new(.:format) temoignages#new
edit_temoignage GET /temoignages/:id/edit(.:format) temoignages#edit
temoignage GET /temoignages/:id(.:format) temoignages#show
PUT /temoignages/:id(.:format) temoignages#update
DELETE /temoignages/:id(.:format) temoignages#destroy
admin_petitions GET /admin/petitions(.:format) admin/petitions#index
POST /admin/petitions(.:format) admin/petitions#create
new_admin_petition GET /admin/petitions/new(.:format) admin/petitions#new
edit_admin_petition GET /admin/petitions/:id/edit(.:format) admin/petitions#edit
admin_petition GET /admin/petitions/:id(.:format) admin/petitions#show
PUT /admin/petitions/:id(.:format) admin/petitions#update
DELETE /admin/petitions/:id(.:format) admin/petitions#destroy
admin_contents GET /admin/contents(.:format) admin/contents#index
POST /admin/contents(.:format) admin/contents#create
new_admin_content GET /admin/contents/new(.:format) admin/contents#new
edit_admin_content GET /admin/contents/:id/edit(.:format) admin/contents#edit
admin_content GET /admin/contents/:id(.:format) admin/contents#show
PUT /admin/contents/:id(.:format) admin/contents#update
DELETE /admin/contents/:id(.:format) admin/contents#destroy
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
GET /admin/posts/new(.:format) admin/posts#new
GET /admin/posts/:id/edit(.:format) admin/posts#edit
GET /admin/posts/:id(.:format) admin/posts#show
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy
admin_backend_index GET /admin/backend(.:format) admin/backend#index
POST /admin/backend(.:format) admin/backend#create
new_admin_backend GET /admin/backend/new(.:format) admin/backend#new
edit_admin_backend GET /admin/backend/:id/edit(.:format) admin/backend#edit
admin_backend GET /admin/backend/:id(.:format) admin/backend#show
PUT /admin/backend/:id(.:format) admin/backend#update
DELETE /admin/backend/:id(.:format) admin/backend#destroy
lois_index GET /lois/index(.:format) lois#index
lois_show GET /lois/show(.:format) lois#show
edito_index GET /edito/index(.:format) edito#index
reponses_index GET /reponses/index(.:format) reponses#index
reponses_show GET /reponses/show(.:format) reponses#show
lettres_index GET /lettres/index(.:format) lettres#index
lettres_show GET /lettres/show(.:format) lettres#show
accueils POST /accueils(.:format) accueils#create
new_accueils GET /accueils/new(.:format) accueils#new
edit_accueils GET /accueils/edit(.:format) accueils#edit
GET /accueils(.:format) accueils#show
PUT /accueils(.:format) accueils#update
DELETE /accueils(.:format) accueils#destroy
root / accueil#index
See the route names in the left column of your rake routes? Use that info to build calls to different path helpers:
<li><%= link_to 'Contenu', admin_contents_path %></li>
<li><%= link_to 'Petitions', admin_petitions_path %></li>
In general, that's how you should be building URLs in your Rails 3+ app.
Read through Rails Routing from the Outside In for more information (particularly 2.3: Paths and URLs).
Update:
You should look into using these _path-style helpers on the view that's being pulled up when you visit /admin/petitions.
I bet you have a link on the page similar to this:
<%= link_to "Link Text", :controller => "edito", :action => "index" %>
It's trying to find edito in the admin namespace because that's where you are within the app when you visit /admin/petitions.
To fix, you need to update it to read like this:
<%= link_to "Link Text", edito_index_path %>
Wash, rinse, and repeat for all links, forms, and url_for references in your app.

Rails form_for with nested resources: "No route matches"

I have the following nested resources in my routes.rb file. The inner resource specifies a controller name.
resources :batches, :except => [:new], :path => "sets" do
resources :tags, :controller => "batches_tags"
end
In the view for BatchesTags#new, I am trying to build a form:
<%= form_for [#batch, #tag], :url => batch_tag_path do |f| %>
...
<% end %>
Attempting to load this page (/sets/1/tags/new) gives me a ActionController::RoutingError with the message:
No route matches {:action=>"show", :controller=>"batches_tags"}
But when I run $ rake routes, it clearly shows this route does exist:
batch_tag GET /sets/:batch_id/tags/:id(.:format) {:action=>"show", :controller=>"batches_tags"}
Does anyone know how to fix this error?
Edit:
In the view for Batches#show, I use that same batch_tag_path function and it works perfectly:
<%= link_to "...", batch_tag_path(#batch, tag) %>
It turns out that, while batch_tag_path is a valid route (making the "No route matches" error message very confusing), the path I needed was the pluralized batch_tags_path, as seen in this $ rake routes output:
batch_tags GET /sets/:batch_id/tags(.:format) {:action=>"index", :controller=>"batches_tags"}
POST /sets/:batch_id/tags(.:format) {:action=>"create", :controller=>"batches_tags"}
Perhaps the error message meant that batch_tag_path wasn't a valid route for POST?

Rails 3.1 No route matches controller and action but they exist

I'm playing with a test rails app. However I stumbled on a problem I can't resolve.
I've a users controller and in it there's an activate method.
In the routes.rb file I have
match 'activate/:email/:validation_code' => 'users#activate', :as => :activate_user, :via => :get
After that I try to use activate_user_path(#user) but a routing error is raised.
No route matches {:controller=>"users", :action=>"activate", :email=>#<User id: 12, email: "someone#test.test", validation_code: "zbBPLQUsBgPvEJfcjxmXuxFxuJAKEoqQNASkbybihpnmzSbhxdC...", active: false, created_at: "2011-11-10 14:56:23", updated_at: "2011-11-10 14:56:23">}
Running rake routes shows the routing is there:
activate_user GET /activate/:email/:validation_code(.:format) {:controller=>"users", :action=>"activate"}
I tried searching for this problem, but usually this happens when you forget to pass the object that's needed to build the route.
All help is appreciated :)
I would make sure you are actually invoking a GET and not a POST as the method.
If you are submitting a form, it defaults to POST, which would mean you don't have a matching route, the route you listed above only works for GET requests.
Does activate_user_path(:email => #user.email, :validataion_code => #user.validation_code) works for you?
EDIT
I don't know why, but when I've changed:
validation_code to code it works.
match 'activate/:email/:code' => 'users#activate', :as => :activate_user, :via => :get
Probably in rails you can't use var names in routes with underscore, but this has to be verified.

Routing error on Rails 3

I'm working on a profile page, so the config is like this:
routes.rb
=> match "user/:login(*path)" => 'users#profile', :as => :profile
rake routes
=> profile /user/:login(*path)(.:format) {:action=>"profile", :controller=>"users"}
on console
> Rails.application.routes.recognize_path("/user/example/whatever")
=> {:action=>"profile", :login=>"example", :controller=>"users", :path=>"/whatever"}
And I have a profile action in UsersControllers.
But when I use
&lt%= link_to user.name, profile_path(user.login) %>
in a view I get the error
No route matches {:login=>"example", :controller=>"users", :action=>"profile"}
What am I missing?
Thanks
Update:
Thanks for the answer and attention, Steve!
After a lot of time trying, a coworker find what I was missing: the problem was only with some logins that are emails too, with "#", ".", etc. The solution was adding to_url at params[:login] in link_to:
&lt%= link_to 'name', profile_path(params[:login].to_url) %>
Again, thanks for the attention!
Using your configuration, the route and helper seem to work fine in a Rails 3.0.5 sample app.
I verified the route helper profile_path in the Rails console:
>> app.profile_path('example')
=> "/user/example"
and checked that it worked in the view as well:
<%= link_to 'name', profile_path(params[:login]) %>
No route errors in either place. And putting <%= debug(params) %> in the view shows the path '/user/example/whatever' is being parsed correctly:
--- !map:ActiveSupport::HashWithIndifferentAccess
controller: users
action: profile
login: example
path: /whatever