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

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

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

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

Q&A

解決済

1回答

283閲覧

undefined method `id' for nil:NilClassのエラーが出ました。

asdfghjkl123

総合スコア1

Ruby on Rails 6

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

0グッド

0クリップ

投稿2021/05/26 14:51

rails 初心者です。
エラーが解決できません・・・
よろしくお願い致します

イメージ説明

コントローラー

class ImagesController < ApplicationController
layout "logged_in"
before_action :redirect_to_login_unless_logged_in

def index
@title = "投稿一覧"
@user = current_user
@image = Image.where(user_id: current_user.id)
end
end

ビュー
<p><%= @user %>さん、こんにちは!</p>
<ul> <% @image.each do |image| %> <li><%= image.comment %> (<%= image.created_at %>)</li> <% end %> <% if @image.empty? %> <li>書き込みはありません。</li> <% end %> </ul>

ルーティング

Rails.application.routes.draw do

root "images#index"

get "/images/index", to: "images#index", as: :index

get "/images/new", to: "images#create", as: :new_image

end

アプリケーションコントローラ

class ApplicationController < ActionController::Base

private
def redirect_to_login_unless_logged_in
redirect_to static_path if current_user.nil?
end

def current_user
User.find(session[:user_id]) if session[:user_id]
end
end

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

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

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

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

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

guest

回答1

0

ベストアンサー

before_action :redirect_to_login_unless_logged_in
これが機能していないのでは?
loginしていない状態でindexにアクセスしてlogin画面に行きますか?

もしそれで行くとすると、method current_userがおかしいです。その場合はそこのcode載せてください

投稿2021/05/26 23:04

編集2021/05/26 23:05
winterboum

総合スコア23329

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

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

asdfghjkl123

2021/05/27 09:30

エラーを消すため <ul> <% @image.each do |image| %> <li><%= image.comment %> (<%= image.created_at %>)</li> <% end %> <% if @image.empty? %> <li>書き込みはありません。</li> <% end %> </ul> と @user = current_user @image = Image.where(user_id: current_user.id) はコメントアウトしております ログアウトした状態でindexに飛べました・・・
asdfghjkl123

2021/05/27 09:33

class StaticPageController < ApplicationController layout "not_logged_in" def new @title = "ログイン" end def create # 該当のメールアドレスのユーザーを取得 user = User.find_by(email: params[:email]) if !log_in?(user, params[:password]) # ログイン認証に失敗した場合はメッセージを表示し再度ログインフォームを表示。 flash[:danger] = "ログインに失敗しました" redirect_to static_path and return end # 認証に成功したら、セッションにユーザーidを保存 session[:user_id] = user.id # トップページ(投稿一覧)に遷移 redirect_to root_url end def destroy # セッションデータからuser_idを削除 session.delete(:user_id) flash[:success] = "ログアウトしました" redirect_to static_path end private def log_in?(user, password) user && user.authenticate(password) end end class ImagesController < ApplicationController layout "logged_in" # 各アクションの実行前にログインチェック before_action :redirect_to_login_unless_logged_in def index @title = "投稿一覧" # ログイン中ユーザーの取得 # @user = current_user # @image = Image.where(user_id: current_user.id) end def show @title = "投稿の詳細" end def new @title = "新規投稿" @image = Image.new end def create @image = Image.new(images_params) @image.user_id = current_user.id @image.image = "" if !@image.save flash[:danger] = "投稿の追加に失敗しました。" @title = "新規投稿" render :new and return end flash[:success] = "投稿を追加しました" redirect_to action: :index end private def images_params params.require(:images).permit( :comment ) end end
winterboum

2021/05/27 22:40

解決したのかな?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問