I'm using the http://guides.rubyonrails.org/getting_started.html as an example to help me create my own application. I create the blog and comments modules just fine. When I add a method to the comments or blog controllers I cannot get a link_to action to work calling the new function. Everything points to a problem in the routes.rb but I've tried all the new syntax I've seen and nothing is working for me.
What I'm trying to do is create a simple execute method in the controller to run a ruby script and save the output to the database. Everything works according to the tutorial but when I try to extend the comment controller with a custom function called execute I cant get that to run.
comments_controller.rb #Same as destroy
def execute
#post = Post.find(params[:post_id])
#comment = #post.comments.find(params[:id])
#comment.destroy
redirect_to post_path(#post)
end
_comment.html.erb
<%= link_to 'Execute Comment', [comment.post, comment],
:method => :execute %>
routes.rb
resources :posts do
resources :comments do
get :execute, :on => :member
end
end
rake routes |grep execute
execute_post_comment GET /posts/:post_id/comments/:id/execute(.:format) {:action=>"execute", :controller=>"comments"}
Error when I click Execute comment link:
No route matches "/posts/3/comments/6"
run rake routes and see if there are any routes pointing to your controller action. If not you'll need to create one either as a "member action" or with a match rule.
If you do see the route, you can name it by passing an :as => route_name parameter to the routing rule. Doing so will enable the route_name_path() and route_name_url() helpers for your link_to
RailsCasts has a good quick rundown of the rails 3 routing syntax here
EDIT:
based on the code examples, try this :
<%= link_to 'Execute Comment', execute_post_comment_path(comment.post, comment) %>
According to the docs here the :method option can only contain valid http verbs (get, put, post, delete). The link_to helper can't puzzle out which action you want to hit with a custom member action, so you have to use the named route as above.
HTH
Related
I have an app where I'm creating a get action called "new_911". When I put new_911_call_path in the application layout I get an error "no route matches new_911 controller: calls". Yet there is an action in the calls controller called new_911. What am I doing wrong?
Calls Controller:
def new_911
#call = Call.new :call_status => "open"
respond_with #call
end
application.html.erb
<li><%= link_to 'New 911 Call', new_911_call_path %></li>
routes.rb
resources :calls do
member do
post 'close'
post 'cancel'
post 'note'
get 'new_return'
get 'duplicate_call'
get 'edit_times'
put 'update_billing'
get 'new_911'
end
rake routes:
new_911_call GET /calls/:id/new_911(.:format) calls#new_911
You need to add the parameter to the route. You're using a member route so you need to add the id parameter, take a look of this. You may need to change that route.
Figured it out. I was using a member instead of a collection. Also using new_911 gave me a constant error so I changed it to EmergencyCalls for my controller schema and utilized the normal "new" action. Added resources :emergency_calls to my routes file and it worked.
Sorry for the goof.
We are testing a customerx engine under spec/dummy. Index page of the engine customerx can be displayed without any error. Here is the link:
<li><%= link_to 'Customers', customerx.customer_status_categories_path %></li>
However there is routing error uninitialized constant CustomerStatusCategoriesController for new customer link as below:
<li><%= link_to 'New Customer', customerx.new_customer_status_category_path %></li>
The rake routes does show the right new customer route:
Routes for Customerx::Engine:
customer_status_categories_index GET /customer_status_categories/index(.:format) customer_status_categories#index
customer_status_categories_new GET /customer_status_categories/new(.:format) customer_status_categories#new
customer_status_categories_create GET /customer_status_categories/create(.:format) customer_status_categories#create
customer_status_categories_edit GET /customer_status_categories/edit(.:format) customer_status_categories#edit
customer_status_categories_update GET /customer_status_categories/update(.:format) customer_status_categories#update
customer_status_categories GET /customer_status_categories(.:format) customerx/customer_status_categories#index
POST /customer_status_categories(.:format) customerx/customer_status_categories#create
new_customer_status_category GET /customer_status_categories/new(.:format) customerx/customer_status_categories#new
edit_customer_status_category GET /customer_status_categories/:id/edit(.:format) customerx/customer_status_categories#edit
customer_status_category PUT /customer_status_categories/:id(.:format) customerx/customer_status_categories#update
In routes.rb for engine customerx, the resources is declared as:
resources :customer_status_categories, :only => [:index, :new, :create, :edit, :update]
There is no routing error with edit/index. The rspec cases for new/create all passes. The problem seems that the action for new is not found (the error is the same after deleting def of new and create).
What could be wrong in code causing the error? Thanks for help.
The problem was solved by commenting out get "customer_status_categories/new" in routes.rb for engine customerx:
Customerx::Engine.routes.draw do
get "customer_status_categories/index"
#get "customer_status_categories/new"
get "customer_status_categories/create"
get "customer_status_categories/edit"
get "customer_status_categories/update"
resources :customer_status_categories
root :to => 'customer_status_categories#index'
end
The get ...new in routes.rb was inserted by rails generate automatically when creating new controller. We don't know why this line causes uninitialized constant error (but not on others such as index and edit). Can Someone shed the light on the cause of the issue?
Im just looking for some clarification on the following piece of code, well part of it.To give some background i have an app where you can upload recipes, search recipes and save them as favourites, this piece of code is in a controller "recipes", action is "my_recipes"
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create', :recipe_id => recipe.id}, {:method => :post } %>
My understanding is that this creates a link_to (anchor tag if you will) that makes a post request through the create method within the favourites controller. This part I think i underdstand (corrections welcome), the part i am unsure of is
:recipe_id => recipe.id}
I know this is passing the recipe_id for example but I would like to know why we do this? and what relevance of the : before the first recipe_id.May seem obvious to some but you dont know until you learn.
Any help appreciated
Is this code in a partial? Is recipe being passed along? You should rewrite as so:
link_to "Add to favorites", new_favourite_path(recipe), method: :post
Do rake routes in your console and find out what the path is for creating favourites, then replace 'new_favourite' with that above. Note, the route might be identified with something more explicit like new_favourite_recipe.
To answer you question, you must pass recipe, or recipe.id because otherwise the controller wouldn't know which recipe to add to the favourites. You don't need to specify the user as that should be accessed directly from within the controller action using something like current_user.
I created a scaffold without problems
$ rails generate scaffold New name:string title:string content:text
Rake command to run the migration (no problems as before, table correctly created)
$ rake db:migrate
Edit app/views/home/index.html.erb
<%= link_to 'My News', :controller => 'news' %>
I see the home and the link correctly in "http://localhost:3000"; clicking the link "My news" page "http://localhost:3000/news" is loaded without errors.
Now, clicking in the link "New New" generated by Rails, link's target localhost:3000/news/new (source "<a href ="/news/new">New New</ a>"), i read this error:
Routing Error
No route matches {:action=>"show", :controller=>"news", :format=>nil}
Try running rake routes for more information on available routes.
In "app/views/news/index.html.erb" the link souce is
<%= link_to 'New New', new_news_path %>
In routes.rb i read
MyApp::Application.routes.draw do
resources :news
get "home/index"
Rails 3.2.3
Ruby 1.9.3p125
MySQL 5.5
Windows 7 64 bit
Rakes routes:
news_index GET /news(.:format) news#index
POST /news(.:format) news#create
new_news GET /news/new(.:format) news#new
edit_news GET /news/:id/edit(.:format) news#edit
news GET /news/:id(.:format) news#show
PUT /news/:id(.:format) news#update
DELETE /news/:id(.:format) news#destroy
home_index GET /home/index(.:format) home#index
root / home#index
Thanks in advance and sorry for my English
you have to use news_index_path because news is not singular if rails can't make singular - prular distinguish they will add _index at the end :)
You have one news and many news and this is confusing.
Always try to use <name_of_resource>_path to generate urls :)
news_index GET /news(.:format) news#index
This says it implicit, you use 1 part news_index and add _path to get path for it.
You should have
<%= link_to 'My News', news_index_path %>
Hope that helps, cheers!
here my git https://gist.github.com/774510
the problem is if I'm using method:destroy in the _comments.html.erb I get sent to articles/1/comments, where no route matches, furthermore the comment doesn't get deleted
if I use method:delete, I get routed to the right page articles/1, but instead of deleting the comment, my app creates a new one :/
I believe you need to tell rails that the comment is nested under articles OR just delete the comment in the comments controller:
1.)
# routes.rb
resources :articles do
resources :comments
end
2.)
# _comments.html.erb
<%= link_to 'delete' comment, :method => :delete %>