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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

2733閲覧

別のユーザーがeditアクションに遷移しようとするとトップページに戻るようにしたい

gomappi

総合スコア7

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/01/16 05:34

前提・実現したいこと

それぞれのユーザーが画像やテキストを投稿できるシステムを作成しています。
タイトルにように、他のユーザーがeditアクションに遷移しようとするとトップページに戻るようにしたいのですが、以下のようなエラーが発生してしまいました。

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

NameError in PrototypesController#edit undefined local variable or method `prototypes' for #<PrototypesController:0x00007fb854e9c7d0> Did you mean? prototypes_url def move_to_index unless user_signed_in? && current_user.id == prototypes.user_id redirect_to action: :index end end

該当のソースコード

prototypes.controller

ruby

1class PrototypesController < ApplicationController 2 before_action :authenticate_user! , only: [:new,:edit,:destroy] 3 before_action :move_to_index , only: [:edit] 4 5 def index 6 @prototypes = Prototype.all 7 end 8 9 def new 10 @prototype = Prototype.new 11 end 12 13 def create 14 if Prototype.create(prototype_params) 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 def show 22 @prototype = Prototype.find(params[:id]) 23 @comment = Comment.new 24 @comments = @prototype.comments.includes(:user) 25 end 26 27 def edit 28 @prototype = Prototype.find(params[:id]) 29 end 30 31 def update 32 prototype = Prototype.find(params[:id]) 33 if prototype.update(prototype_params) 34 redirect_to prototype_path 35 else 36 render edit_prototype_path 37 end 38 end 39 40 def destroy 41 prototype = Prototype.destroy(params[:id]) 42 redirect_to root_path 43 end 44 45 private 46 def prototype_params 47 params.require(:prototype).permit(:title,:catch_copy,:concept,:image).merge(user_id: current_user.id) 48 end 49 50 def move_to_index 51 unless user_signed_in? && current_user.id == prototypes.user_id 52 redirect_to action: :index 53 end 54 end 55 56end 57

prototypes/edit.html.erb

ruby

1 <div class="main"> 2 <div class="inner"> 3 <div class="form__wrapper"> 4 <h2 class="page-heading">プロトタイプ編集</h2> 5 <%# 部分テンプレートでフォームを表示する %> 6 <%= render partial: "form",locals: {prototype:@prototype} %> 7 </div> 8 </div> 9</div>

prototypes/_prototype.html.erb

ruby

1 <div class="card"> 2 <%= link_to image_tag(prototype.image, class: :card__img), prototype_path(prototype.id) %> 3 <div class="card__body"> 4 <%= link_to prototype.title, prototype_path(prototype.id), class: :card__title%> 5 <p class="card__summary"> 6 <%= prototype.catch_copy %> 7 </p> 8 <%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %> 9 </div> 10</div>

試したこと

prototypes.controllerのmove_to_indexメソッドを記述中に発生したので、ユーザーIDの紐付けがうまくいっていないと考察したが、どこを直せばいいのか分からない。

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

ruby 2.6.5
Rails 6.0.3.4

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

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

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

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

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

guest

回答1

0

自己解決

prototypes.controllerの

ruby

1 def move_to_index 2 unless user_signed_in? && current_user.id == prototypes.user_id 3 redirect_to action: :index 4 end 5 end

を削除し、

ruby

1 def edit 2 @prototype = Prototype.find(params[:id]) 3 unless @prototype.user_id == current_user.id 4 redirect_to action: :index 5 end 6 end

に変更したらちゃんと動作することに成功しました。

投稿2021/01/16 07:23

gomappi

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問