2

I am the newbie of the ROR and I am going through the Ruby on Rails official guide (4.2.6), but I got one problem when I want to add the Article model.

When I am trying to save the article I got the error,

undefined method `article_url' for # Did you mean? articles_url

I found that the route don't have the "article" prefix in my route:

majiandeMacBook-Pro:blog majian$ bin/rake routes
Running via Spring preloader in process 26766
       Prefix Verb   URI Pattern              Controller#Action
welcome_index GET    /welcome/index(.:format) welcome#index
         root GET    /                        welcome#index
     articles POST   /articles(.:format)      articles#create
 new_articles GET    /articles/new(.:format)  articles#new
edit_articles GET    /articles/edit(.:format) articles#edit
              GET    /articles(.:format)      articles#show
              PATCH  /articles(.:format)      articles#update
              PUT    /articles(.:format)      articles#update
              DELETE /articles(.:format)      articles#destroy

But in the document, I found that it should be like this:

article GET    /articles/:id(.:format)      articles#show

Does anybody know why the routes are different? Any help will be appreciated.

Jason Ma
  • 71
  • 2

3 Answers3

2

Check your routes.rb file, it should look like this:

The file is in config/routes.rb

Rails.application.routes.draw do
  get 'welcome/index'
  resources :articles
  root 'welcome#index'

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

The error can be caused by an error in the line "resources :articles"

1

I ran into the same issue, and it was because I have misspelled the resources :article in routes.rb I had resource :article instead and that doesn't work

Naji
  • 11
  • 1
0

article_url requires as an argument the id of the article, in you show action you should have something like this

article_url(@article)
  • There is no `article` (singular) route so this won't work. I believe the OP uses wants to use singular (`article`) instead of plural (`articles`). So this is an issue of `resource` vs `resources`. – Vucko May 13 '16 at 10:03
  • You are right, maybe there is something wrong with his routes.rb – Emiliano Della Casa May 13 '16 at 10:25
  • I've found the error, in the routes.rb,. I set the " resource :articles" instead of "resources :articles", I don't even know it can work when I misspelled. – Jason Ma May 17 '16 at 05:48