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

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

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

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

Ruby on Rails

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

Q&A

解決済

2回答

2011閲覧

投稿者以外のユーザーが投稿者専用のページに遷移できないように設定したい。

gannbaritai

総合スコア2

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/09 15:29

#実現したいこと
投稿者以外のユーザーが投稿者専用の編集画面にURLを使って遷移することを制限、
投稿者本人のIDでのみ編集ページに遷移できる仕組みにしたい。

###相談したいこと
こちらの実装の確認でURLを指定して投稿者編集ページ(edit.html.erb)に飛ぼうとするとエラーになります。
ここのエラーを解除して記述したとりの動きを実現したいです。
###エラー内容

NoMethodError in PrototypesController#edit undefined method `id' for nil:NilClass

###コード一覧
PrototypesController.rb

class PrototypesController < ApplicationController before_action :authenticate_user!, only: [:show, :index, :new] before_action :move_to_index, except: [:index, :show] def index @prototypes = Prototype.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]) prototype.update(prototype_params) if prototype.save redirect_to root_path else 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 @prototype_user.id == current_user.id redirect_to action: :index end end end

UsersController.rb

class UsersController < ApplicationController def show @user = User.find(params[:id]) @prototypes = @user.prototypes end end

_prototype.html.erb

<div class="card"> <%= link_to prototype_path(prototype) do %> <%= image_tag (prototype.image) %> <% end %> <div class="card__body"> <%= link_to "#{prototype.title}", root_path, class: :card__title%> <p class="card__summary"> <%= "#{prototype.catch_copy}" %> </p> <%= link_to "by #{prototype.user.name}", "/users/#{prototype.user.id}", class: :card__user %> </div> </div>

prototypes/edit.html.erb

<div class="main"> <div class="inner"> <div class="form__wrapper"> <h2 class="page-heading">プロトタイプ編集</h2> <%= render partial: "form" %> </div> </div> </div>

index.html.erb

<main class="main"> <div class="inner"> <% if user_signed_in? %> <div class="greeting"> <%= "こんにちは" %> <%= link_to current_user.name, "/users/#{current_user.id}", class: :greeting__link %> </div> <% end %> <div class="card__wrapper"> <%= render partial: 'prototype', collection: @prototypes %> </div> </div> </main>

show.html.erb

<main class="main"> <div class="inner"> <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= "#{@prototype.title}" %> </p> <%= link_to "by #{@prototype.user.name}", user_path(@prototype.user.id), class: :prototype__user %> <% if user_signed_in? && current_user.id == @prototype.user_id %> <div class="prototype__manage"> <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %> <%= link_to "削除する", prototype_path(@prototype.id), method: :delete, class: :prototype__btn %> </div> <% end %> <div class="prototype__image"> <%= image_tag (@prototype.image) %> </div> <div class="prototype__body"> <div class="prototype__detail"> <p class="detail__title">キャッチコピー</p> <p class="detail__message"> <%= "#{@prototype.catch_copy}" %> </p> </div> <div class="prototype__detail"> <p class="detail__title">コンセプト</p> <p class="detail__message"> <%= "#{@prototype.concept}" %> </p> </div> </div> <div class="prototype__comments"> <% if user_signed_in? %> <%= form_with(model: [@prototype, @comment], local: true) do |f| %> <div class="field"> <%= f.label :text, "コメント" %><br /> <%= f.text_field :text %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <% end %> <ul class="comments_lists"> <% if @comments %> <% @comments.each do |comment| %> <li class="comments_list"> <%= comment.text %> <%= link_to "#{comment.user.name}", "/users/#{comment.user.id}", class: :comment_user %> </li> <% end %> <% end %> </ul> </div> </div> </div> </main>

users/show.html.erb

<div class="main"> <div class="inner"> <div class="user__wrapper"> <h2 class="page-heading"> <%= "#{@user.name}さんの情報"%> </h2> <table class="table"> <tbody> <tr> <th class="table__col1">名前</th> <td class="table__col2"><%= "#{@user.name}" %></td> </tr> <tr> <th class="table__col1">プロフィール</th> <td class="table__col2"><%= "#{@user.profile}" %></td> </tr> <tr> <th class="table__col1">所属</th> <td class="table__col2"><%= "#{@user.occupation}" %></td> </tr> <tr> <th class="table__col1">役職</th> <td class="table__col2"><%= "#{@user.position}" %></td> </tr> </tbody> </table> <h2 class="page-heading"> <%= "#{@user.name}さんのプロトタイプ"%> </h2> <div class="user__card"> <%= render partial: 'prototypes/prototype', collection: @prototypes %> </div> </div> </div> </div>

routes.rb

Rails.application.routes.draw do devise_for :users root to: "prototypes#index" resources :prototypes do resources :comments, only: :create end resources :users, only: :show end

###仮説と行った作業
nillクラスと出ているので何かしらデータが空なのだと思いどこのデータがないのかを確認しました。

結果

PrototypesController.rbに記述している

def move_to_index unless @prototype_user.id == current_user.id redirect_to action: :index end end

こちらの、prototype_user.idのデータがnill状態でした。

原因はここかなと断定しているのですが、どこでこうなっているのか
なぜ、空の状態なのか、どうすれば中身のデータをここに渡せるのかが自分で見つけられなかったため
お力をお借りしたく質問しました。
どうかよろしくお願いします。

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

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

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

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

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

guest

回答2

0

ベストアンサー

下記のような形でいかがでしょうか?

Ruby

1before_action :move_to_index, only: [:edit, :update, :destroy] 2# newやcreateの場合はprototypeは登録されていない状態なので除外します。

Ruby

1def move_to_index 2 prototype = Prototype.find(params[:id]) 3 if prototype.user_id != current_user.id 4 redirect_to action: :index 5 end 6end 7# 見落としていたら恐縮なのですが、ご提示頂いたソースの@prototype_userという変数はどこから出てきたのでしょうか?

投稿2020/11/09 23:32

no1knows

総合スコア3365

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

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

gannbaritai

2020/11/10 00:00

ありがとうございます。確かにどこから出てきた変数なのかよく考えると不思議でたまりません! こちらの記述で無事エラー解除ができました!
no1knows

2020/11/10 00:42

それは良かったです!
guest

0

URLパラメータ渡すのではなく、固定のURLでログインセッションからID持ってくれば良いのでは。
つまり、アクセスURLも /users/1とかではなく/user
一般的なSNSでもよく用いられる手法です。

投稿2020/11/09 22:17

m.ts10806

総合スコア80850

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問