質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

243閲覧

undefined method `user_id' for nil:NilClass

T-yosi

総合スコア0

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/08/23 06:52

前提・実現したいこと

アカウントを作り投稿した後に、再度ホーム画面(index)ビューに戻ろうとしたときにエラーが出た。

発生している問題・エラーメッセージ

NoMethodError in Products#index undefined method `user_id' for nil:NilClass

該当のソースコード

_

1<div class="contents row"> 2 <% if user_signed_in? && current_user.id == @product.user_id %> 3 <div class="more"> 4 <ul class="more_list">
class ProductsController < ApplicationController before_action :set_product, only: [:edit, :show] before_action :move_to_index, except: [:index, :show] def index @products = Product.includes(:user).order("created_at DESC") end def new @product = Product.new end def create Product.create(product_params) end def destroy product = Product.find(params[:id]) product.destroy end def edit end def update product = Product.find(params[:id]) product.update(product_params) end def show @comment = Comment.new @comments = @comment.comments.includes(:user) end private def product_params params.require(:product).permit(:product, :price, :exp_text, :image).merge(user_id: current_user.id) end def set_product @product = Product.find(params[:id]) end def move_to_index unless user_signed_in? redirect_to action: :index end end end

試したこと

補足情報(FW/ツールのバージョンなど)

rails 6.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2020/08/24 10:46

「アカウントを作り投稿した後に、再度ホーム画面(index)ビューに戻ろうとした」のはどのactionですか? cerate? ここにはindeexへのrenderがないですね?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問