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

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

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

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

Q&A

解決済

2回答

1047閲覧

詳細ページを表示させたい

0W5E8fPq1EOm4yE

総合スコア13

Ruby on Rails

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

0グッド

0クリップ

投稿2020/11/24 14:00

前提・実現したいこと

投稿したものの詳細ページを表示させようとしたらエラーがおきました。
rails routesではprototype_pathだったのであっているはずなのですが

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

イメージ説明

ActionController::UrlGenerationError in Prototypes#index

該当のソースコード

show.html.erb <main class="main"> <div class="inner"> <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= "プロトタイプのタイトル"%> </p> <%= link_to "by プロトタイプの投稿者", root_path, class: :prototype__user %> <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> <% if user_signed_in? && current_user.id == prototype.user_id %> <div class="prototype__manage"> <%= link_to "編集する", edit_prototype_path, class: :prototype__btn %> <%= link_to "削除する", root_path, method: :delete, class: :prototype__btn %> <% end %> </div> <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> <div class="prototype__image"> <%= image_tag.prototype.image, class: :card__img %> </div> <div class="prototype__body"> <div class="prototype__detail"> <p class="detail__title">キャッチコピー</p> <p class="detail__message"> <%= "プロトタイプのキャッチコピー" %> </p> </div> <div class="prototype__detail"> <p class="detail__title">コンセプト</p> <p class="detail__message"> <%= "プロトタイプのコンセプト" %> </p> </div> </div> <div class="prototype__comments"> <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> <%= form_with local: true do |f|%> <div class="field"> <%= f.label :concept, "コメント" %><br /> <%= f.text_field :concept %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <%# // ログインしているユーザーには上記を表示する %> <ul class="comments_lists"> <%# 投稿に紐づくコメントを一覧する処理を記述する %> <li class="comments_list"> <%= " コメントのテキスト "%> <%= link_to "( ユーザー名 )", root_path, class: :comment_user %> </li> <%# // 投稿に紐づくコメントを一覧する処理を記述する %> </ul> </div> </div> </div> </main>
prototypes_controller.rb class PrototypesController < ApplicationController #CSRF保護を無効にする protect_from_forgery with: :null_session before_action :move_to_index, except: [:index, :show] def index @prototype = Prototype.all end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :new @prototype = Prototype.includes(:user) end end def show @prototype = Prototype.find(params[:id]) end def edit @prototype = Prototype.find(params[:id]) end def update prototype = prototype.find(params[:id]) prototype.update(prototype_params) if prototype.save redirect_to root_path(@prototype) else @prototype = @user.prototype.includes(:user) render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params[:prototype].permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end def move_to_index unless user_signed_in? redirect_to action: :index end end end

試したこと

投稿した内容の詳細を表示させようとしたのですがActionController::UrlGenerationErrorとなりました。
rails routesコマンドでは詳細ページはprototype_pathになるのですがそれでもエラーとなりました。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

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

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

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

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

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

guest

回答2

0

自己解決

_prototype.html.rb prototype_path ↓ prototype_path(prototype.id)

に変更したところ詳細ページが表示されました。

投稿2020/11/25 12:40

0W5E8fPq1EOm4yE

総合スコア13

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

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

0

実際のルーティングを見ていないので恐らくにはなりますが、

prototype_path(params[:id]) や edit_prototype_path(params[:id])

などになるかと思います。
paramsの中にshowまたはeditの対象idが入っているかと思います。

如何でしょうか?

投稿2020/11/24 14:50

tomtomtomtom

総合スコア563

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

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

0W5E8fPq1EOm4yE

2020/11/25 11:31

現在のルーティングです! Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / prototypes#index prototypes GET /prototypes(.:format) prototypes#index POST /prototypes(.:format) prototypes#create new_prototype GET /prototypes/new(.:format) prototypes#new edit_prototype GET /prototypes/:id/edit(.:format) prototypes#edit prototype GET /prototypes/:id(.:format) prototypes#show PATCH /prototypes/:id(.:format) prototypes#update PUT /prototypes/:id(.:format) prototypes#update DELETE /prototypes/:id(.:format) prototypes#destroy rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
0W5E8fPq1EOm4yE

2020/11/25 12:39 編集

_prototype.html.rb prototype_path ↓ prototype_path(prototype.id) に変更したところ詳細ページが表示されました! どうもありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問