Rails 3 No route matches {:action=>"show", error - ruby-on-rails-3

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

Related

explicit get route is throwing an error

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)

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.

url_for adding controller and action to querystring in rails 3.2

I am trying to generate a url in an actionmailer template. An example if the url I want to generate is
http://0.0.0.0:3000/users/confirm/lNbQxzFukYtEEw2RMCA
Where the last segment is a hash to identify the user
However when I use this
<%= url_for(:controller => 'users', :action => 'confirm', :id => #user.confirmhash, :only_path => false) %>
It generates this
http://0.0.0.0:3000/assets?action=confirm&controller=users&id=ZOR3dNMls8533T8hJUfCJw
How can I get it to correctly format? I have no idea where 'assets' is coming from.
Is there an easier way to use named routes that I am missing?
I've found the answer. As I'm still learning I've missed the option to create a named route. So this this the path I've taken.
In config/routes.rb
match 'user/confirm/:id' => 'users#confirm', :as => :confirm_account
Then in my action mailer template I've used
<%= link_to "Confirm your account", confirm_account_url(#user.confirmhash) %>
Which passes the :id into the controller action.

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?

No route matches {:action=>"destroy", :controller=>"users"}

I'm getting a error when I try to acces users#show page trough a named route (http://localhost:3000/profile/) ... otherwise I have no error when I try to access it with the standard url (http://localhost:3000/users/current). If I rake routes I routes seems right and since it works with standard url, I really have no clue why I get No route matchs error. When why trying to find route match for action 'destroy' when I'm not even trying to access it?
Starcast::Application.routes.draw do
match "login" => 'user_sessions#new', :as => :login
match "logout" => 'user_sessions#destroy', :as => :logout
resources :user_sessions
match "profile" => 'users#show'
resources :users
resources :casters
resources :casts
resources :orders
root :to => "home#index"
end
Error I get:
ActionView::Template::Error (No route matches {:action=>"edit", :controller=>"users"}):
1: <% title "Welcome #{#user.username}" %>
2:
3: <%= link_to "Edit your profil", edit_user_path %>
4:
5: <% has_role? :caster do %>
6: <% if #user.caster %>
app/views/users/show.html.erb:3:in `_app_views_users_show_html_erb___2116234531537545622_2170017780__3613739707062673465'
Edit/show/destroy/update paths require an id parameter ... i.e. edit_user_path(current_user.id) ... If you don't want to do it that way you'll need to make your routes use resource :user (instead of resources :user) which will cause lots of headache later down the road if you don't do it right.