Reputation: 1
articles_controller.rb:
class ArticlesController < ApplicationController
def show
@article = Article.find(params[:id])
end
end
show.html.erb:
<h1>Showing article details</h1>
<p><strong>Title: </strong><%= @article.title %></p>
<p><strong>Description: </strong><%= @article.description %></p>
schema.rb:
ActiveRecord::Schema.define(version: 2020_04_08_043002) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at"
t.datetime "updated_at"
end
end
routes.rb:
Rails.application.routes.draw do
root 'pages#home'
get 'about', to: 'pages#about'
resources :articles, only: [:show]
end
New to Ruby so I'm not sure what I did wrong, any help is greatly appreciated!
Upvotes: 0
Views: 56
Reputation: 98
I don't see any problem with the code. When you create a route like resources :articles, only: [:show]
it generates a GET method like /articles/:id
. So when you call the show action make sure you have value for id
included in the path, like localhost:3000/articles/779
for example.
If the id is not present in the path then the controller doesn't know the value of params. ie params[:id]
will be nil
. This makes @article = nil
. Thus @article.title
leads to this error in your view. Please check if this is the possible error happening.
Upvotes: 1