L
L
lavezzi12015-02-22 10:25:28
Ruby on Rails
lavezzi1, 2015-02-22 10:25:28

How to fix errors when opening a post before creating a user?

Hello. Making an app using this tutorial www.youtube.com/watch?v=70Pu_28yvdI
I'm stuck at the moment when the author creates a user (by installing gem advise). So, I did everything on video. Works only on new posts, that is, when created already as a user. And the old posts do not open at all, the error is as follows:

NoMethodError in Posts#show
Showing /home/roman/muse/app/views/posts/show.html.haml where line #5 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #5):
3
4
5
6
7
8
%p= post .link
%p= post .description
%p= post .user.name
= link_to "Edit", edit_post_path(@post)
= link_to "Delete", post_path(@post), method: : delete, data: { confirm: "Are you sure?" }
Rails.root: /home/roman/muse
Application Trace | Framework Trace | full trace
app/views/posts/show.html.haml:5:in `_app_views_posts_show_html_haml___2652678237944587056_70251639245740'
Request
Parameters:
{"id"=>"2"}

Here is the post_controller.rb file code
class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @posts = Post.all.order("created_at DESC")
  end

  def show
  end

  def new
    @post = current_user.posts.build
  end

  def create
    @post = current_user.posts.build(post_params)

    if @post.save
      redirect_to @post
    else
      render 'new'
    end
  end

  def edit
  end

  def update
    if @post.update(post_params)
      redirect_to @post
    else
      render 'edit'
    end
  end

  def destroy
    @post.destroy
    redirect_to root_path
  end

  private

  def find_post
    @post = Post.find(params[:id])
  end

  def post_params
    params.require(:post).permit(:title, :link, :description, :image)
  end
end

show file code
= image_tag @post.image.url(:medium)
%h1= @post.title
%p= @post.link
%p= @post.description
%p= @post.user.name

= link_to "Edit", edit_post_path(@post)
= link_to "Delete", post_path(@post), method: :delete, data: { confirm: "Are you sure?" }

Already checked everything 20 times. Please help!
UPD. id different
2.2.0 :006 > Post.first
  Post Load (1.2ms)  SELECT  "posts".* FROM "posts"  ORDER BY "posts"."id" ASC LIMIT 1
 => #<Post id: 1, title: "", link: "", description: "", created_at: "2015-02-21 17:43:16", updated_at: "2015-02-22 05:49:10", user_id: 1> 
2.2.0 :007 > Post.last
  Post Load (0.6ms)  SELECT  "posts".* FROM "posts"  ORDER BY "posts"."id" DESC LIMIT 1
 => #<Post id: 6, title: "Amazon.com Redesign", link: "#", description: "Try to retouch amazon.com homepage with material d...", created_at: "2015-02-22 06:36:12", updated_at: "2015-02-22 06:43:43", user_id: 1>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ID25, 2015-02-22
@lavezzi1

Это потому, что у старых постов нет пользователя, который их создал. Либо через консоль добавь им user.id, либо просто очисти БД от всех записей.

O
OnYourLips, 2015-02-22
@OnYourLips

Нужно выполнить rake db:reset
Это пересоздаст базу и заполнит ее изначальными данными.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question