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!
Related
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 upgrading a rails 2 app to rails 3 app and am having a bunch of routing issues. Heres the current:
So in the page its trying to load (results/_form.html.erb) I have a form with the following syntax:
<%= form_tag(:controller => "results",:action => "show") do %>
And i do have an action in my results_controller.rb called show. Yet i keep getting the no route matches error. Is this rails 2 syntax and not 3? Is there something I need to do in my routes.rb file? I think there is because that was a a major change between rails 2 and 3, im just not sure what. Any suggestions?
show action ideally should expect id to be passed in the params:
<%= form_tag(:controller => "results",:action => "show", :id => #user.id) do %>
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 %>
I'm working through Michael Hartl's excellent tutorial on Rails, but I am having trouble with exercise 7 in Chapter 11.
This exercise is:
Add a nested route so that
/users/1/microposts shows all the
microposts for user 1. (You will also
have to add a Microposts controller
index action and corresponding view.)
I've done this successfully by changing my routes.rb file to read:
resources :users do
resources :microposts, :only => [:create, :destroy]
end
I am able to successfully call /users/1/microposts from a browser. However, most of the tests in microposts_controller_spec.rb are now broken. I receive the "no route matches" error when running autotest. For instance, the first test, which simply reads:
it "should deny access to 'create'" do
post :create
response.should redirect_to(signin_path)
end
now produces the following error:
1) MicropostsController access
control should deny access to 'create'
Failure/Error: post :create
No route matches {:controller=>"microposts",
:action=>"create"}
When I check rake routes
, I find this entry:
user_microposts POST /users/:user_id/microposts(.:format) {:action=>"create", :controller=>"microposts"}
which suggests the route does exist.
Has anyone else run into this issue while completing the tutorial? Is there a change I need to make in the spec file once I introduce nested routes? Does Rspec work with nested routes?
thanks
Because this is a nested route you will need to pass the user_id through:
some_user = way_of_creating_a_user_goes_here
post :create, :user_id => some_user.id
RSpec will attempt to go to the /microposts route without this parameter.
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