前提・実現したいこと
ユーザーがログイン中のみマイページの編集・削除を許可するようにしたいです。
しかし、実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
NoMethodError in Users#show undefined method `user_id' for nil:NilClass
該当のソースコード
questions/show.html.erb
Ruby
1<div class="contents row"> 2 <p><%= @nickname %>さんの質問一覧</p> 3 4 <% @questions.each do |question| %> 5 <div class="content_post"> 6 <%= simple_format(question.title) %> 7 </div> 8 <% end %> 9 10 <% if user_signed_in? && current_user.id == @question.user_id %> 11 <%= link_to '編集', edit_question_path(@question) %> | 12 <%= link_to '削除', "/questions/#{@question.id}", method: :delete %> | 13 <% end %> 14 15</div> 16
class UsersController < ApplicationController def show user = User.find(params[:id]) @nickname = user.nickname @questions = user.questions end def edit end def destory end end
###補足情報
下記のコードではうまく動いてます。まだRailsの理解ができていないのですが、やはりユーザーコントローラーに何か書き加える必要性があるのでしょうか?ご教授お願いします。
class QuestionsController < ApplicationController before_action :set_question, only: [:show, :edit, :update, :destroy] before_action :move_to_index, except: [:index, :show] # GET /questions # GET /questions.json def index @questions = Question.includes(:user) end # GET /questions/1 # GET /questions/1.json def show end # GET /questions/new def new @question = Question.new end # GET /questions/1/edit def edit end # POST /questions # POST /questions.json 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 # PATCH/PUT /questions/1 # PATCH/PUT /questions/1.json 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 # DELETE /questions/1 # DELETE /questions/1.json 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 # Use callbacks to share common setup or constraints between actions. def set_question @question = Question.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. 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
users/show.html.erb↓↓↓
<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 %>
回答1件
あなたの回答
tips
プレビュー