rails routes error in the Official Guide - ruby-on-rails

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.

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"

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

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

Related

undefined method `comments_path' which I can't find in the code files

I am getting the error below (NoMethodError) while trying to add a new comment to an article , the problem is that it refers to undefined method `comments_path' which I can't find in the code files
Please help
Note:
I have tried to search about this error , but results I found were not relevant also the problem is the error is pointing to something I can't find.
The error is shown below:
NoMethodError in Comments#new
Showing /home/abc/my_ruby_projects/myblog3/app/views/comments/_form.html.erb where line #1 raised:
undefined method `comments_path' for #<#:0x007fb57888bf28>
Did you mean? font_path
Extracted source (around line #1):
<%= form_with model: #comment do |form| %>
<% if comment.errors.any? %>
<div id="error_explanation">
<....>
<ul>
Trace of template inclusion: app/views/comments/new.html.erb
Rails.root: /home/abc/.../myblog3
I have defined nested routes for articles & comments as shown below:
resources :articles do
resources :comments
end
my routes seems correct , as shown below:
Prefix Verb URI Pattern Controller#Action
rails_admin /admin RailsAdmin::Engine
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
Your form is looking for a route to which it can post the comment updates.
Your routes config likely needs something like
resources :comments
A command line call to list routes will now show you this comments_path it's looking for
rake routes
And then you'll need a CommentsController with an update method/action to handle the postback.
class CommentsController
def edit
end
def update
# save data
end
end
If you're trying to add comments to an article I imagine you have a nested resource where an Article has many comments. Now your /config/routes.rb would have an entry like:
resources :articles do
resources :comments
end
and assuming there is accept_nested_attributes in your /models.article.rb model.
Now in your view usually your form should look something similar to:
<%= form_for [#article, #comment] do |f| %>
When routing I find it useful using the rails routes command, but once they grow too much instead of piping the output to grep I'd rather use the gem sextant in development.

rails generates wrong 'show' route

i have following show function:
def show
#article = Article.find(params[:id])
end
my routes.rb looks like this:
resource :articles
but when i run rake routes, i get this output:
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
root GET /
as you can see the articles#show route is wrong because an :id is needed to show a single article.
resource :articles
should be
resources :articles
But you have discovered what resource method does :)

scaffold routes do not expect to have id's

When I run rake routes in rails 5 app I see the following urls available:
edit_articles GET /articles/edit(.:format) articles#edit
articles GET /articles(.:format) articles#show
PATCH /articles(.:format) articles#update
PUT /articles(.:format) articles#update
DELETE /articles(.:format) articles#destroy
this whay when I create a link_to a resource I need to include id param in it like:
link_to article.name, manager_articles_path(id: article.id)
instead of rails 4 way:
link_to article.name, manager_articles_path(article)
how can I make rails 5 routes to behave as rails 4 ones?
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
Thank you.
routes.rb
Rails.application.routes.draw do
root 'home#index'
resource :articles
end
In rails resource and resources is not same.
resource
http://guides.rubyonrails.org/routing.html#singular-resources
Sometimes, you have a resource that clients always look up without
referencing an ID. For example, you would like /profile to always show
the profile of the currently logged in user. In this case, you can use
a singular resource to map /profile (rather than /profile/:id) to the
show action.
routes
edit_articles GET /articles/edit(.:format) articles#edit
articles GET /articles(.:format) articles#show
PATCH /articles(.:format) articles#update
PUT /articles(.:format) articles#update
DELETE /articles(.:format) articles#destroy
resources
resources is used as a way to handle generic requests on any item, then a singular resource is a way to work on the current item at hand.
routes
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
I hope this will help you.

Missing :id routes on resource creation

My rake routes after creating the resource :articles says I should get:
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / welcome#index
However my routes are always missing those with :id in them.
Prefix Verb URI Pattern Controller#Action
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
root GET / welcome#index
I guess you have added to the routes:
resource :articles
instead of:
resources :article
Singular resource is used when you don't have to provide id to identify resource. Ie.: when you operate on current user's profile: /profile

undefined local variable or method `welcome_goodbye_path' in Rails?

I am a beginner in rails and trying to learn using "Agile Web Development using Rails".I wanted to create a link to a webpage.This is my code:
<h1>Hello, Rails!</h1>
<p>
It is now <%= Time.now %>
</p>
<p>
Time to say
<%= link_to "Goodbye", welcome_goodbye_path %>!
</p>
But this gives error...
undefined local variable or method `welcome_goodbye_path'
What am I doing wrong ?
This is the code of my welcome controller:
class WelcomeController < ApplicationController
def index
end
def goodbye
end
end
This is the result of rake routes:
Prefix Verb URI Pattern Controller#Action
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
welcome_index GET /welcome(.:format) welcome#index
POST /welcome(.:format) welcome#create
new_welcome GET /welcome/new(.:format) welcome#new
edit_welcome GET /welcome/:id/edit(.:format) welcome#edit
welcome GET /welcome/:id(.:format) welcome#show
PATCH /welcome/:id(.:format) welcome#update
PUT /welcome/:id(.:format) welcome#update
DELETE /welcome/:id(.:format) welcome#destroy
root GET /
You'd have to show config/routes.rb. You need something in there like:
get 'goodbye', to: 'welcome#goodbye', as: 'welcome_goodbye'

Resources