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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

2682閲覧

編集画面へのリンクをクリックすると『undefined method `user_id' for nil:NilClass』と表示されてしまいます

yasu0205

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/04/04 04:44

編集画面へのリンクをクリックすると『undefined method `user_id' for nil:NilClass』と表示されてしまいます。

https://gyazo.com/58b87aa87c0e985a4fcf712935238488

前提・実現したいこと

(prototypeの)投稿者以外が(prototypeの)編集画面にURLから直接アクセスできない様にしたい!
※直接アクセスした際はトップページへ

【この実装に向けて行ったこと】

①prototypeコントローラー内にのprivateにてmove_to_indexメソッド作成

②before_actionnにてeditアクションにのみ設定

☆説明不足な箇所等あるかもしれませんが、ぜひ皆様のお力をお貸しください

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

https://gyazo.com/58b87aa87c0e985a4fcf712935238488
NoMethodError in PrototypesController#edit undefined method `user_id' for nil:NilClass

該当のソースコード

class PrototypesController < ApplicationController before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy] before_action :move_to_index, only: [:edit] def index @prototype = Prototype.all.includes(:user) end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path else render :new end end def show @prototype = Prototype.find(params[:id]) @comment = Comment.new @comments = @prototype.comments.includes(:user) end def edit @prototype = Prototype.find(params[:id]) end def update prototype = Prototype.find(params[:id]) if prototype.update(prototype_params) redirect_to prototype_path(prototype.id) else @prototype = Prototype.find(params[:id]) render :edit end end def destroy @prototype = Prototype.find(params[:id]) @prototype.destroy redirect_to root_path end private def prototype_params params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end def move_to_index unless user_signed_in? && current_user.id == @prototype.user_id redirect_to action: :index end end end
class CreatePrototypes < ActiveRecord::Migration[6.0] def change create_table :prototypes do |t| t.string :title, null: false t.text :catch_copy, null: false t.text :concept, null: false t.references :user, foreign_key: true t.timestamps end end end

試したこと

sequelpro、マイグレーションファイルにて prototypeにuse_idカラムがあるかチェック(外部キー制約)

prototypeコントローラ内のeditアクションチェック

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

rails 6.0.0

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

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

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

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

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

guest

回答1

0

ベストアンサー

unless user_signed_in? && current_user.id == @prototype.user_id

ここで使用している@prototypeはまだ定義されていないのでnilになっているかと思われます。
つまりnil.user_idになってしまっており、undefined methodになっているのかなと。

解決するためには、@prototypeが定義されてから行うといいと思われます。

たとえば、editアクションを以下のようにして

def edit @prototype = Prototype.find(params[:id]) unless user_signed_in? && current_user.id == @prototype.user_id redirect_to action: :index end end

↓これは削除するといかがでしょうか

before_action :move_to_index, only: [:edit]

投稿2021/04/04 04:50

hatsu

総合スコア1809

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

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

yasu0205

2021/04/04 05:11

hatsuさんクリアできました!大変助かりました ご説明も大変わかり安かったです ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問