rails generates wrong 'show' route - ruby-on-rails

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 :)

Related

Needed to fill in the questions for URI path, controller, and the action/method after I place in routes commands, but I wasn't sure want to look for

The pertinent line in the output from the rails routes command for dealing with listing all articles is as follows:
articles GET /articles(.:format) articles#index
The URI path to your new view in the above line is _____________.
The controller is ___________.
The action or method in the above controller is _____________
*********OUTPUT FROM BASH COMMAND************************
ec2-user:~/environment/ruby_labs/blog (master) $ rails routes
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
new_articles GET /articles/new(.:format) articles#new
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
POST /articles(.:format) articles#create
root GET / welcome#index
article GET /articles/:id(.:format) articles#show
GET /articles(.:format) articles#index
rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
The question above reviews rails conventions and file structure.
You can learn more about it here: https://guides.rubyonrails.org/v2.3/routing.html
The answers to above the questions would be the following:
The URI path to your new view in the above line is /articles/index
The controller is articles_controller
The action or method in the above controller is #index

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.

rails routes error in the Official Guide

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)

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