Noeland
Noeland

Reputation: 1

Routing Error after scaffolding and migration

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"

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

Upvotes: 0

Views: 879

Answers (1)

Jakub Oboza
Jakub Oboza

Reputation: 5421

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!

Upvotes: 1

Related Questions