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

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

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

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

Ruby on Rails 6

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

Q&A

解決済

1回答

476閲覧

Railsで、ActiveRecord::RecordNotFoundとRouting Errorが同時に発生してしまった

athlaliel

総合スコア7

Ruby

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

Ruby on Rails 6

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

0グッド

0クリップ

投稿2020/10/13 06:09

編集2020/10/20 14:34

似たような投稿がテラテイルにあるなか、どうしても解決にこぎつけず投稿しました。

前提・実現したいこと

①別ユーザーが投稿したTweetの詳細から、ユーザー一覧へ遷移したい
②自分の投稿したTweetの詳細から、ユーザー一覧に遷移したい

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

投稿アプリでNameErrorを解消したら、新たに2つのエラーが同時に出現してしまった。
どちらもユーザー一覧への遷移なのですが、エラーが違うのは、ログインしているユーザーの違いと考えました。
配置したユーザー一覧のリンクを押下すると下記エラーが発生します。

イメージ説明

イメージ説明

該当のソースコード

ソースコード

routes.rb↓

Rails.application.routes.draw do devise_for :users resources :users, only: [:show, :index] devise_scope :user do post 'users/guest_sign_in', to: 'users/sessions#new_guest' end root to: 'tweets#index' resources :tweets do resources :comments, only: :create resources :likes, only: [:create, :destroy] collection do get 'search' end end # get 'maps/index' # resources :maps, only: [:index] end

tweets_controller.rb↓

class TweetsController < ApplicationController before_action :set_tweet, only: [:edit, :show] # before_action :move_to_index, except: [:index, :show, :search] def index @tweets = Tweet.includes(:user).order("created_at DESC").all.page(params[:page]).per(8) end def new @tweet = Tweet.new end def create @tweet = Tweet.new(tweet_params) if @tweet.save flash[:notice] = "投稿が完了しました" redirect_to root_path else flash.now[:alert] = 'メッセージを入力してください。' render :new end end def destroy tweet = Tweet.find(params[:id]) tweet.destroy flash[:notice] = "投稿を削除しました" redirect_to root_path end def edit end def update @tweet = Tweet.find(params[:id]) @tweet.update(tweet_params) if @tweet.save flash[:notice] = "更新が完了しました" redirect_to root_path else render :new end end def show @comment = Comment.new @comments = @tweet.comments.includes(:user) end def search @tweets = Tweet.search(params[:keyword]) respond_to do |format| format.html format.json end end private def tweet_params params.require(:tweet).permit(:image, :text, :genre_id).merge(user_id: current_user.id) end def set_tweet @tweet = Tweet.find(params[:id]) end def move_to_index unless user_signed_in? redirect_to action: :index end end end

users_contoroller.rb↓

class UsersController < ApplicationController def index @users = User.all end def show @user = User.find(params[:id]) @nickname = @user.nickname @tweets = @user.tweets end end

users/index.html.erb↓

<div class="user-list"> <h1>ユーザー一覧</h1> <% @users.each do |user| %> <a href="/users/<%= user.id %>"><%= user.nickname %>"><%= user.email %></a> <hr> <% end %> <%= link_to "ホームへ戻る", root_path %> </div>

users/show.html.erb

div class="contents row"> <p><%= @nickname %>さんの投稿一覧</p> <% @tweets.each do |tweet| %> <%= render partial: "tweets/tweet", locals: { tweet: tweet } %> <% end %> </div> <%# <%= link_to "ユーザー一覧へ", users_path %> <%= link_to "ホームへ戻る", root_path %> <div class ="footer"> <%= image_tag asset_path("pan8.jpg", alt: "画像は表示されていますか?"), class: "footer__view"%> <h1 class="footer__spel">あなたの好きなパンは何ですか?</h1> </div>

tweets/show.html.erb

<div class="contents row"> <div class="content_post" style="background-image: url(<%= @tweet.image %>);"> <% if user_signed_in? && current_user.id == @tweet.user_id %> <div class="more"> <span><%= image_tag 'arrow_top.png' %></span> <ul class="more_list"> <li> <%= link_to '編集', edit_tweet_path(@tweet.id), method: :get %> </li> <li> <%= link_to '削除', tweet_path(@tweet.id), method: :delete %> </li> </ul> </div> <div id="likes_buttons<%= @tweet.id %>"> <%= render partial: 'likes/like', locals: { tweet: @tweet, like: @like} %> </div> <% end %> <p><%= @tweet.text %></p> <span class="name"> <a href="/users/<%= @tweet.user.id %>"> <span>投稿者</span><%= @tweet.user.nickname %> </a> </span> </div> <div class="container"> <% if current_user %> <%= form_with(model: [@tweet, @comment], local: true, id: "new_comment") do |form| %> <%= form.text_area :text, placeholder: "コメントする", rows: "2", class: "textbox" %> <%= form.submit "SEND", value: "コメントを登録する", class: "form__submit" %> <% end %> <% else %> <strong><p>※※※ コメントの投稿には新規登録/ログインが必要です ※※※</p></strong> <% end %> <div class="comments"> <h4><コメント一覧></h4> <% if @comments %> <% @comments.each do |comment| %> <p> <strong><%= link_to comment.user.nickname, "/users/#{comment.user_id}" %>:</strong> <%= comment.text %> </p> <% end %> <% end %> </div> </div> </div> <%= render partial: "footer" %>

tweets/edit.html.erb

<div class="contents row"> <div class="container"> <%= form_with(model: @tweet, local: true) do |form| %> <h3>編集する</h3> <% @tweet.errors.full_messages.each do |message| %> <div class="form-error"> <%= message %> </div> <% end %> <%= render partial: "form", locals: { form: form } %> <% end %> </div> </div> <%= render partial: "footer" %>

試したこと

Routing Errorと出ているのでroute.rbのアクションが記述されていないのが原因かと思ったのですが、
どちらもresourcesを使っているので特に制限はないはずで原因が特定できておりません。

MVCの流れを意識してそれぞれのファイルを追っておりますが原因は現状不明です。

結局は同じページに行きたいのに別エラーが起きているのは、route.rbではなく、コントローラーの記述ミスが原因と見るべきなのでしょうか?

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

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

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

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

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

winterboum

2020/10/18 04:43

エラーの原因はエラーが起きた所ではなくそれ以前に問題がある事が多いです。 ので、 エラーはどの画面でどの操作をした時かが判るようにしてください。 ひとつ目は users/index.html.erb の <a href="/users/<%= user.id %>">ですか? ふたつ目はここには載っていない?
athlaliel

2020/10/20 14:32

コメント頂いたのに返信遅れ申し訳ありません。 一つ目のエラーはtweets/show.html.erbから、users/index.html.erbに移動したい場合に発生します。 (単一投稿からユーザー一覧ページに移動しようとした場合) 2つ目のrouting errorはtweets/edit.html.erbからusers/index.html.erbへ移動したい場合に発生します。(投稿編集からユーザー一覧に移動した場合です。)
guest

回答1

0

自己解決

indexのパスを修正したところ、どの画面からもユーザー一覧に遷移できるようになりました。

投稿2020/10/20 15:10

athlaliel

総合スコア7

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問