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

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

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

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

Q&A

解決済

1回答

944閲覧

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

joruju1988

総合スコア10

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/09 09:13

前提・実現したいこと

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

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

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 %>

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

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

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

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

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

no1knows

2020/02/09 11:45

認証には、Deviseを使っていますか?それとも他のgemまたは独自で実装したのでしょうか?
joruju1988

2020/02/09 11:52

Deviseを使っています!
guest

回答1

0

ベストアンサー

コントローラーに下記を記載するとログインが必須となります。

controller

1before_action :authenticate_user!

https://github.com/heartcombo/devise#controller-filters-and-helpers

下記がわかりやすいかもしれません。
Rails deviseで使えるようになるヘルパーメソッド一覧

投稿2020/02/09 12:08

no1knows

総合スコア3365

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

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

joruju1988

2020/02/10 02:40

回答ありがとうございます。 https://qiita.com/tobita0000/items/866de191635e6d74e392 を見ながら試してみました。すると以下のようなエラーが出ました。 ActionController::UrlGenerationError in Users#show No route matches {:action=>"edit", :controller=>"questions", :id=>nil}, missing required keys: [:id] ``` <% if user_signed_in? %> <%= link_to '編集', edit_question_path(@question) %> | <%= link_to '削除', "/questions/#{@question.id}", method: :delete %> | <% end%> ```
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問