-2

getting the undefined method 'title' error in my show view. This has been posted and resolved several in other posts but for seemingly other problems than mine.

I have also double checked that I can read from the database through the rails console and everything looks fine.

Loading development environment (Rails 4.2.3)
irb(main):001:0>
irb(main):002:0*
irb(main):003:0* post = Post.find(4)
Post Load (1.0ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" =   $1 LIMI
T 1  [["id", 4]]
=> #<Post id: 4, title: "Why Title Why", body: "Where is the Body??", category_i
d: nil, author_id: nil, created_at: "2015-08-12 14:49:38", updated_at: "2015-08-
12 14:49:38">
irb(main):004:0> post.title
=> "Why Title Why"
irb(main):005:0>

routes.rb

Rails.application.routes.draw do
 get 'categories/index'

 get 'categories/edit'

 get 'categories/new'

 get 'categories/show'

 get 'home/index'
   resources :posts
   resources :categories


 root 'home#index'

database.yml

default: &default
adapter: postgresql
username: postgres
password: aaaaa
pool: 5
timeout: 5000

development:
<<: *default
database: mysecondblog

Post_controller

class PostsController < ApplicationController

def index
    @posts = Post.all

end

def show

  @posts = Post.find(params[:id])

end

def new

end

def create

end

def edit

end

def update

end


def destroy

end

private 

def post_params
    params.require(:post). permit( :title, :body, :category_id, :author_id)
end

end

posts\show.html.erb

<h1><%= @post.title %></h1>
<small><%= @post.created %></small>
<p><%= @post.body %></p>

thanks for any help

billybeb
  • 13
  • 4

1 Answers1

0

You have @posts variable (@posts = Post.all) inside controller that could be accessed from view.

When you are rendering view file (show.html.erb) - you ask view to find variable @post with parameter .title, .created, .body - @posts inside controller and @post in view - different variables.

So, you need change your view show.html.erb

<h1><%= @posts.title %></h1>
<small><%= @posts.created_created %></small>
<p><%= @posts.body %></p>

or variable inside function of Controller:

def index
    @post = Post.all
end

def show
  @post = Post.find(params[:id])
end
YUzhva
  • 316
  • 2
  • 13
  • Really appreciate it. That fixed it. I know its really simple debug stuff but its just taking a while to know what to look for. spent yesterday and this morning stuck on this. – billybeb Aug 12 '15 at 16:02