Why does "link_to" work but not "button_to" - ruby-on-rails-3

Why would this work?
<%= link_to "New Item", new_site_care_path, {:class => "button_bottom"} %>
And this (the only change is from link_to to button_to)...
<%= button_to "New Item", :url => new_site_care_path, {:class => "button_bottom"} %>
...produces:
No route matches "/site_cares/new"
UPDATE - ROUTE INFO -
route file:
resources :site_cares, :except => :show
rake routes:
new_site_care GET /site_cares/new(.:format) {:action=>"new", :controller=>"site_cares"}

I think that button_to uses post by default, my ROR route skills are a bit rusty but I think the route you've shown uses GET.
There's further info in this question - Button_to in Ruby on Rails bad route

Related

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.

Working through Rails3 routes

I have the following when I do rake routes
next_post /users/next_post/:index(.:format) {:controller=>"users", :action=>"next_post"}
then, when I have this code in my ERB file:
<%= link_to "next", :next_post %>
I get the following error, and I just can't figure it out
No route matches {:controller=>"users", :action=>"next_post"}
What am I doing wrong here? The route itself looks like this
match '/users/next_post/:index',
:controller => "users",
:action => 'next_post',
:as => :next_post
You should use this
<%= link_to "next", next_post_path(#post.id + 1) %>

no route matches even after adding route to routes.rb

This is the code in the view.
<% form_tag({:controller => 'users',
:action => 'test'}) do %>
<%= text_field_tag(:search_options, params[:search_options])%>
<%= submit_tag("Display text!")%>
<% end -%>
I have a file test.html.erb and have also added get "users/test" to routes.rb still i'm getting Error: No route matches "/users/test"
The form_tag method creates a form to be send using HTTP POST by default. You state that the route you define in your routes.rb is a GET. So you have two options to fix this problem:
Change your route to POST "users/test"
Change your form_tag call to: form_tag({:controller => 'users', :action => 'test'}, :method => :get)

How to change the controller path of a link in rails 3?

How do I get:
<%= link_to 'Back', originalcontrollers_path %>
to be:
<%= link_to 'Back', modifiedcontrollers_path %>
I already have my route updated with:
get "modifiedcontrollers", :to => "originalcontrollers#index"
So the "/modifiedcontroller" url works the same as "/originalcontroller". When creating links I need it to point to the new url. How would I do this?
I am not sure I understand you, but try this:
get "modifiedcontrollers", :to => "originalcontrollers#index", :as => :modifiedcontrollers
get "modifiedcontrollers/new", :to => "originalcontrollers#new", :as => :new_modifiedcontroller
...
so if you need all stack of routes:
resources :modifiedcontrollers, :controller => :originalcontrollers
so now this will work as expected
<%= link_to 'Back', modifiedcontrollers_path %>

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.