undefined local variable or method `welcome_goodbye_path' in Rails? - ruby-on-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'

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

NoMethodError undefined method ` ' for nil:NilClass

I'm novice with rails and starting with http://guides.rubyonrails.org/getting_started.html stucked in 5.7 Showing Articles with following error:
NoMethodError in Articles#show
Showing /home/jakub/workspace/blog/blog/app/views/articles/show.erb where line #3 raised:
undefined method `title' for nil:NilClass
Where the source is :
<p>
<strong>Title:</strong>
<%= #article.title %>
</p>
<p>
and articles_controller.rb is:
class ArticlesController < ApplicationController
def index
#articles = Article.all
end
def create
#article = Article.new(article_params)
#article.save
redirect_to #article
end
private
def article_params
params.require(:article).permit(:title, :text)
end
def show
#article = Article.find(params[:id])
end
end
and rake routes command brings:
Prefix Verb URI Pattern Controller#Action
welcome_contact GET /welcome/contact(.:format) welcome#contact
welcome_index GET /welcome/index(.:format) welcome#index
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
Any idea what might cause this issue?
Where should I look for ?
Your show action is private, therefore the instance variable #post cannot be used in the view. Move it to a public scope and the problem should be fixed.
Move your show action to public block.
undefined method `***' for nil:NilClass is actually shown when you are trying to access a object property which is actually not created yet.
Always check on view like this:
#atrile.title if #article.title.present?

Resources