Rails form_for with nested resources: "No route matches" - ruby-on-rails-3

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?

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

Nested Routes Broken: Rails 3

I'm having a devil of a time getting this particular nested route to work. It's odd, because I've been migrating a number of routes to the new Rails 3 syntax and this one in particular just doesn't seem to work. Here goes.
I've got an object called "piece" which has a nested object called "piece_comment". Here's what the routes.rb looks like:
resources :piece do
resources :piece_rating, :as => :rating
resources :piece_comments, :as => :comments
end
And here is what piece/show.html.erb looks like, with a form to submit a piece comment:
<% #piece_comment = PieceComment.new(:piece_id => #piece.id, :user_id => current_user.id) %>
<%= form_for [#piece, #piece_comment] do |f| %>
<%= f.hidden_field 'piece_comment', 'user_id' %>
<%= f.hidden_field 'piece_comment', 'piece_id' %>
<%= f.text_area 'piece_comment', 'comment' %>
<%= f.submit_tag 'Post' %>
<% end %>
Now, what's weird is that I get the following error triggered by the "form_for" line:
undefined method `piece_piece_comments_path' for #<#<Class:0x007f80ec732a48>:0x007f80ec737ae8>
Shouldn't the :as in my routes file be sending it to piece_comments_path, and not piece_piece_comments_path? if I change it to :as => :foobar or something, I get the same error. So clearly the routes file would not seem to be working correctly. (Oddly, the behavior of the rating route seems fine.)
Any ideas for what might be wrong with the routing?
Altough I'm not sure it is the problem, resources should be plural in the routes.rb. Try with:
resources :pieces do
resources :piece_ratings, :as => :ratings
resources :piece_comments, :as => :comments
end
Use rake routes to see the name of the routes generated by the routes.rb.

Routing error (GET instead of POST) with Vote_fu/Thumbs_up gem

I am trying to use the vote_fu gem and I encounter a problem that other have as well, but I can't solve this with others' solutions..
I don't think the problem is related directly to the gem.. but rather to some wrong route or a missing parameter..
I have a Msg model which I included the act_as_voteable.
For the User Model I added act_as_voter
In the Msg controller I added
def votefor
#msg= Msg.find(params[:id])
current_user.vote_for(#msg)
redirect_to :back
end
In routes:
resources :msgs do
member do
post :votefor
end
end
And to the show of Msg I added
<%= link_to "Vote Up", votefor_msg_path(#msg), :method => :post %>
But when I click on the link created I get
Routing Error
No route matches [GET] "/msgs/1/votefor"
Why does it 'GET' instead of 'POST'? What am I missing?
I think its very late to answer this question & I mostly think you must have figured it out... I ran into the same issue & I understood the reason why..
This is a sample comment from routes.rb
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
the post 'toggle' here is not the post-controller but :method => 'post'
link_to default uses :method => 'get' but you can override it using :method => 'post'
if u use link_to .. in the example without :method => 'post'
your routing should be
resources :msgs do
member do
get :votefor
end
end
Hope this helps!
Update:
For the doubt you have:
link_to with a :method => :post
use this
<%= link_to "Vote Up", votefor_msg_path(#msg), :method => :post %>
and in the routes it should be
resources :msgs do
member do
post :votefor
end
end
the post :votefor .. is :method => :post not the posts controller.
Do check for brady8's answer

Rails3 Routing Error. :method => :delete, It will be "no route matches"

I want to call a destroy action. It is already define in controller.
The model that I want to destroy is nested resource. But route is existing in rake routes result.
new_content_model GET /contents/:content_id/model/new(.:format) {:controller=>"models", :action=>"new"}
edit_content_model GET /contents/:content_id/model/:id/edit(.:format) {:controller=>"models", :action=>"edit"}
content_model PUT /contents/:content_id/model/:id(.:format) {:controller=>"models", :action=>"update"}
DELETE /contents/:content_id/model/:id(.:format) {:controller=>"models", :action=>"destroy"}
And I call destroy action from this url
<%= link_to "destroy nested model", content_model_path( #content.id, #model.id ), :confirm => "are you sure?", :method => :delete %>
It is no route matches.
Routing Error
No route matches "/contents/1/model/1"`
Please tell me some solutions.
Already wrote <%= javascript_include_tag :all %> in layouts/application.html.erb
Rails 3 link_to (:method => :delete) not working
I solved this problem.
It was wrong that I deleted rails.js and prototype.js.
Thank you for your attending. Sorry for lack of information.

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.