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

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

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

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

Q&A

解決済

2回答

2184閲覧

ユーザーがログイン中のみマイページの編集・削除を許可するようにしたいです。

joruju1988

総合スコア10

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/10 03:22

前提・実現したいこと

ユーザーがログイン中のみマイページの編集・削除を許可するようにしたいです。
しかし、実装中に以下のエラーメッセージが発生しました。

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

ActionController::UrlGenerationError in Users#show No route matches {:action=>"edit", :controller=>"questions", :id=>nil}, missing required keys: [:id]

該当のソースコード

users/show.html.erb

Rails

1<% if user_signed_in? %> 2 <%= link_to '編集', edit_question_path(@question) %> | 3 <%= link_to '削除', "/questions/#{@question.id}", method: :delete %> | 4<% end%>

Rails

1Rails.application.routes.draw do 2 devise_for :users 3 root to: 'questions#index' 4 resources :questions 5 resources :users, only: :show 6end

Rails

1class UsersController < ApplicationController 2 before_action :authenticate_user!, only:[:edit, :destory] 3 4 def index 5 end 6 7 def show 8 user = User.find(params[:id]) 9 @nickname = user.nickname 10 @questions = user.questions 11 end 12 13 def edit 14 flash[:notice] = "ログイン済ユーザーのみ記事の編集ができます" unless user_signed_in? 15 end 16 17 def destory 18 flash[:notice] = "ログイン済ユーザーのみ記事の削除ができます" unless user_signed_in? 19 end 20 21end 22

補足情報

question.contorollerとquestions/show.html.erbはこんな感じで書いていて、ちゃんと動いてくれています。

class QuestionsController < ApplicationController before_action :set_question, only: [:show, :edit, :update, :destroy] before_action :move_to_index, except: [:index, :show] def index @questions = Question.includes(:user) end def show end def new @question = Question.new end def edit end def create @question = Question.new(question_params) respond_to do |format| if @question.save format.html { redirect_to @question, notice: 'Question was successfully created.' } format.json { render :show, status: :created, location: @question } else format.html { render :new } format.json { render json: @question.errors, status: :unprocessable_entity } end end end def update respond_to do |format| if @question.update(question_params) format.html { redirect_to @question, notice: 'Question was successfully updated.' } format.json { render :show, status: :ok, location: @question } else format.html { render :edit } format.json { render json: @question.errors, status: :unprocessable_entity } end end end def destroy @question.destroy respond_to do |format| format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' } format.json { head :no_content } end end private def set_question @question = Question.find(params[:id]) end def question_params params.require(:question).permit(:title, :content).merge(user_id: current_user.id) end def move_to_index redirect_to action: :index unless user_signed_in? end end
<p id="notice"><%= notice %></p> <a href="/users/<%= @question.user.id %>"> <span>質問者</span> <%= @question.user.nickname %> </a> <p> <strong>質問タイトル:</strong> <%= @question.title %> </p> <p> <strong>質問内容:</strong> <%= @question.content %> </p> <% if user_signed_in? && current_user.id == @question.user_id %> <%= link_to '編集', edit_question_path(@question) %> | <%= link_to '削除', "/questions/#{@question.id}", method: :delete %> | <% end %> <%= link_to 'TOPに戻る', questions_path %>

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

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

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

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

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

guest

回答2

0

ベストアンサー

「ユーザーがログイン中のみマイページの編集・削除を許可するようにしたい」
とあり、users/show.html.erbには

<%= link_to '編集', edit_question_path(@question) %> | <%= link_to '削除', "/questions/#{@question.id}", method: :delete %> <% end%>

とあります。マイページとは Questionのことと考えて良いですか?

この users/show.html.erb を呼び出している action show では@questions = user.questions複数形で定義されています。
しかるにviewでは#{@question.id}と単数形です。
userにQuestionが最大一つしかないのか、複数あるのか、によって変わりますが、もし複数あるのなら

<% @questions.each do |question| %> ここにquestionの内容とか必要なもの表示とかもろもろ <% if user_signed_in? %> <%= link_to '編集', edit_question_path(question) %> | <%= link_to '削除', "/questions/#{question.id}", method: :delete %> <% end%> <% end %>``` としてください

投稿2020/02/10 06:45

winterboum

総合スコア23329

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

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

0

解決できました!

ご指摘いただいた通り、

<% @questions.each do |question| %>
<div class="post_title">
<strong>質問タイトル:</strong>
<%= simple_format(question.title) %>
</div>
<div class="post_content">
<strong>質問内容:</strong>
<%= simple_format(question.content) %>
</div>

<% if user_signed_in? && current_user.id == question.user_id %> <%= link_to '編集', edit_question_path(question) %> | <%= link_to '削除', "/questions/#{question.id}", method: :delete %> | <% end%> <%= link_to 'TOPに戻る', questions_path %>

<% end %>

というふうにしたらうまくいきました!ご指摘いただいた所の周辺をよく観察したら、どうしてエラーを起こしていたのか、理解ができました!とても助かりました。ありがとうございました。

投稿2020/02/10 12:34

joruju1988

総合スコア10

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問