Missing :id routes on resource creation - ruby-on-rails

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

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

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.

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'

Routing Error | No route matches [POST] "/subscriptions/create"

I'm new to Ruby on Rails and I'm working on an application that contains form used to create a new nominee ( store it in the database)
I get the problem with this instruction of the form:
<%= form_for :subscription, :url => {:controller => "subscriptions", :action => "create"} do |f| %>
this is the error:
Routing Error
No route matches [POST] "/subscriptions/create"
rake routes
Prefix Verb URI Pattern Controller#Action
subscriptions_create GET /subscriptions/create(.:format) subscriptions#create
subscriptions_index GET /subscriptions/index(.:format) subscriptions#index
articles_index GET /articles/index(.:format) articles#index
new_admin_user_session GET /admin/login(.:format) active_admin/devise/sessions#new
admin_user_session POST /admin/login(.:format) active_admin/devise/sessions#create
destroy_admin_user_session DELETE|GET /admin/logout(.:format) active_admin/devise/sessions#destroy
admin_user_password POST /admin/password(.:format) active_admin/devise/passwords#create
new_admin_user_password GET /admin/password/new(.:format) active_admin/devise/passwords#new
edit_admin_user_password GET /admin/password/edit(.:format) active_admin/devise/passwords#edit
PATCH /admin/password(.:format) active_admin/devise/passwords#update
PUT /admin/password(.:format) active_admin/devise/passwords#update
admin_root GET /admin(.:format) admin/dashboard#index
batch_action_admin_admin_users POST /admin/admin_users/batch_action(.:format) admin/admin_users#batch_action
admin_admin_users GET /admin/admin_users(.:format) admin/admin_users#index
POST /admin/admin_users(.:format) admin/admin_users#create
new_admin_admin_user GET /admin/admin_users/new(.:format) admin/admin_users#new
edit_admin_admin_user GET /admin/admin_users/:id/edit(.:format) admin/admin_users#edit
admin_admin_user GET /admin/admin_users/:id(.:format) admin/admin_users#show
PATCH /admin/admin_users/:id(.:format) admin/admin_users#update
PUT /admin/admin_users/:id(.:format) admin/admin_users#update
DELETE /admin/admin_users/:id(.:format) admin/admin_users#destroy
batch_action_admin_contacts POST /admin/contacts/batch_action(.:format) admin/contacts#batch_action
admin_contacts GET /admin/contacts(.:format) admin/contacts#index
POST /admin/contacts(.:format) admin/contacts#create
new_admin_contact GET /admin/contacts/new(.:format) admin/contacts#new
edit_admin_contact GET /admin/contacts/:id/edit(.:format) admin/contacts#edit
admin_contact GET /admin/contacts/:id(.:format) admin/contacts#show
PATCH /admin/contacts/:id(.:format) admin/contacts#update
PUT /admin/contacts/:id(.:format) admin/contacts#update
DELETE /admin/contacts/:id(.:format) admin/contacts#destroy
admin_dashboard GET /admin/dashboard(.:format) admin/dashboard#index
admin_comments GET /admin/comments(.:format) admin/comments#index
POST /admin/comments(.:format) admin/comments#create
admin_comment GET /admin/comments/:id(.:format) admin/comments#show
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_sponsor GET /welcome/sponsor(.:format) welcome#sponsor
welcome_photo GET /welcome/photo(.:format) welcome#photo
welcome_index GET /welcome/index(.:format) welcome#index
root GET / welcome#index
best regard
As you can see from the lines below:
Prefix Verb URI Pattern Controller#Action
subscriptions_create GET /subscriptions/create(.:format) subscriptions#create
you have got a route for subscriptions_create but it needs to be POST instead of GET. Simply update this route in your routes.rb file in your config folder to be a POST instead of a GET.
As you can see from this line
subscriptions_create GET /subscriptions/create(.:format), that route is only available when using GET HTTP method.
You need to change your routes.rb file for subscriptions to accept POST instead of GET. The easiest way would be to add resources :subscriptions, which will add all standard CRUD routes for resource.

Resources