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

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

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

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

解決済

1回答

2567閲覧

非ログイン時にprototyreごとの詳細ページに移動したい

yozakura10

総合スコア8

Ruby

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

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/10/26 11:46

編集2020/10/26 13:25

1前提・実現したいこと
現在の画面
画面
非ログイン時にprototype(下から2行目のaaaaaaやaaaです、わかりにくてすいません)のタイトルをクリックすると
その詳細ページに移動したい

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

ActionController::UrlGenerationError in Prototypes#show Showing /Users/kamiyaryota/Desktop/projects/protospace-31035/app/views/prototypes/show.html.erb where line #55 raised: No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id] Extracted source (around line #55): 53 54 55 56 57 58 <% @comments.each do |comment| %> <li class="comments_list"> <%= link_to comment.user.name, user_path(current_user), class: :comment_user %>  <%= comment.comment %> <% end %> <% end %> Rails.root: /Users/kamiyaryota/Desktop/projects/protospace-31035 Application Trace | Framework Trace | Full Trace app/views/prototypes/show.html.erb:55 app/views/prototypes/show.html.erb:53 Request Parameters: {"_method"=>"get", "authenticity_token"=>"TS7NO8mBN/tU5/K2D9hpxc8cadsbqTloF+qjxYnHNR/DCFKkdSnbAOzB7l5vnA4OjAupNWORBNQxJM7/FX+aEg==", "id"=>"1"} Toggle session dump Toggle env dump Response Headers: None x >>

3.該当コード
app/views/prototypes/show.html.erb

html

1<main class="main"> 2 <div class="inner"> 3 <div class="prototype__wrapper"> 4 <p class="prototype__hedding"> 5 <%= "プロトタイプのタイトル"%> 6 </p> 7 8 <%= link_to "by #{@prototype.user.name}さん" , user_path(@prototype.user), class: :prototype__user %> 9 <%# 上の記述で@prototypeにuserモデルからnameカラムを持ってくるコードができた %> 10 <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> 11 <% if current_user == @prototype.user%> 12 <div class="prototype__manage"> 13 <%= link_to "編集する", edit_prototype_path(@prototype), class: :prototype__btn %> 14 <%= link_to "削除する", prototype_path(@prototype), method: :delete, class: :prototype__btn %> 15 </div> 16 <% end %> 17 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 18 <div class="prototype__image"> 19 <%= image_tag @prototype.image %> 20 </div> 21 <div class="prototype__body"> 22 <div class="prototype__detail"> 23 <p class="detail__title">キャッチコピー</p> 24 <p class="detail__message"> 25 <%= @prototype.catct_copy%> 26 </p> 27 </div> 28 <div class="prototype__detail"> 29 <p class="detail__title">コンセプト</p> 30 <p class="detail__message"> 31 <%= @prototype.concept%> 32 </p> 33 </div> 34 </div> 35 <div class="prototype__comments"> 36 <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> 37 <% if current_user %> 38 <%= form_with model: [@prototype, @comment], local: true do |f|%> 39 <%# (model: [@prototype, @comment], local: true)にするといいかも %> 40 <div class="field"> 41 <%= f.label :text, "コメント" %><br /> 42 <%= f.text_field :text %> 43 </div> 44 <div class="actions"> 45 <%= f.submit "送信する", class: :form__btn %> 46 </div> 47 <% end %>   48 <% end %> 49 <%# // ログインしているユーザーには上記を表示する %> 50 <ul class="comments_lists"> 51 <%# 投稿に紐づくコメントを一覧する処理を記述する %> 52 <% if @comments %> 53 <% @comments.each do |comment| %> 54 <li class="comments_list"> 55 <%= link_to comment.user.name, user_path(current_user), class: :comment_user %> 56  <%= comment.comment %> 57 <% end %> 58 <% end %> 59 </li> 60 <%# // 投稿に紐づくコメントを一覧する処理を記述する %> 61 </ul> 62 </div> 63 </div> 64 </div> 65</main>

app/views/prototypes/index.html.erb

html

1<main class="main"> 2 <div class="inner"> 3 <%# ログインしているときは以下を表示する %> 4 <% if user_signed_in? %> 5 <div class="greeting"> 6 <%= "こんにちは" %> 7 <%= link_to "#{current_user.name}", user_path(current.user), class: :greeting__link %> 8 <%# <%= link_to "#{current_user.name}", user_registration_path, class: :greeting__link %> 9 </div> 10 <% else %> 11 <div class="grid-6"> 12 <%= link_to "", new_user_session_path, class: "post" %> 13 <%= link_to "", new_user_registration_path, class: "post" %> 14 </div> 15 <% end %> 16 </div> 17 <%# // ログインしているときは上記を表示する %> 18 <div class="card__wrapper"> 19 <%# 投稿機能実装後、部分テンプレートでプロトタイプ投稿一覧を表示する %> 20 <%= render partial: "prototype", collection: @prototypes %> 21 </div> 22 </div> 23</main> 24

app/views/users/show.html.erb

html

1<div class="main"> 2 <div class="inner"> 3 <div class="user__wrapper"> 4 <h2 class="page-heading"> 5 <%= @user.name + "さんの情報" %> 6 </h2> 7 <table class="table"> 8 <tbody> 9 <tr> 10 <th class="table__col1">名前</th> 11 <td class="table__col2"><%= @user.name %></td> 12 </tr> 13 <tr> 14 <th class="table__col1">プロフィール</th> 15 <td class="table__col2"><%= @user.profile %></td> 16 </tr> 17 <tr> 18 <th class="table__col1">所属</th> 19 <td class="table__col2"><%= @user.occupation %></td> 20 </tr> 21 <tr> 22 <th class="table__col1">役職</th> 23 <td class="table__col2"><%= @user.position %></td> 24 </tr> 25 </tbody> 26 </table> 27 <h2 class="page-heading"> 28 <%= @user.name + "さんのプロトタイプ" %> 29 </h2> 30 <div class="user__card"> 31 <%# 部分テンプレートでそのユーザーが投稿したプロトタイプ投稿一覧を表示する %> 32 <%= render partial: "prototypes/prototype", collection: @prototypes %> 33 </div> 34 </div> 35 </div> 36</div>

config/routes.rb

ruby

1Rails.application.routes.draw do 2 devise_for :users 3 root to: "prototypes#index" 4 resources :users, only: [:new, :edit, :update, :show] 5 resources :prototypes, only: [:index, :new, :create, :show, :edit, :update, :destroy,] do 6 resources :comments, only: [:create] 7 end 8 9 devise_scope :user do 10 get '/users/sign_out' => 'devise/sessions#destroy' 11 end 12 13end

app/controllers/prototypes_controller.rb

ruby

1class PrototypesController < ApplicationController 2 before_action :authenticate_user!, except: [:index, :show] 3 4 def index 5 @prototypes = Prototype.all 6 end 7 8 def new 9 @prototype = Prototype.new 10 end 11 12 def create 13 @prototype = Prototype.new(prototype_params) 14 if @prototype.save 15 redirect_to root_path 16 else 17 render :new 18 end 19 end 20 21 def show 22 @prototype = Prototype.find(params[:id]) 23 @comment = Comment.new 24 @comments = @prototype.comments 25 26 end 27 28 def edit 29 @prototype = Prototype.find(params[:id]) 30 unless current_user == @prototype.user 31 redirect_to root_path 32 end 33 end 34 35 def update 36 @prototype = Prototype.find(params[:id]) 37 if @prototype.update(prototype_params) 38 redirect_to prototype_path(@prototype) 39 else 40 render :edit 41 end 42 end 43 44 def destroy 45 prototype = Prototype.find(params[:id]) 46 prototype.destroy 47 redirect_to root_path 48 end 49 50 51 private 52 def prototype_params 53 params.require(:prototype).permit(:title, :catct_copy, :concept, :image).merge(user_id: current_user.id) 54 end 55 56 57end

app/controllers/users_controller.rb

ruby

1class UsersController < ApplicationController 2 before_action :move_to_index, except: [:index, :show] 3 4 5 def index 6 7 end 8 9 def new 10 @user = User.new 11 end 12 13 def edit 14 end 15 16 def show 17 @user = User.find(params[:id]) 18 @prototypes = @user.prototypes 19 end 20 21 22 def move_to_index 23 unless user_signed_in? 24 redirect_to action: :index 25 end 26 end 27 28 29 # private 30 # def user_params 31 # params.require(:user).permit(:name, :profile, :occupation, :position).merge(user_id: current_user.id) 32 # end 33 34end

app/views/prototypes/_prototype.html.erb

html

1<div class="card"> 2 <%= image_tag prototype.image, class: 'card__img' if prototype.image.attached? %> 3 <div class="card__body"> 4 <%= link_to prototype.catct_copy, prototype_path(prototype.id), method: :get, class: :card__title%> 5 <p class="card__summary"> 6 <%= prototype.concept %> 7 </p> 8 <% if current_user %> 9 <%= link_to "by#{prototype.user.name}", user_path(current_user), method: :get, class: :card__user %> 10 <% end %> 11 </div> 12</div> 13 14

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

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

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

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

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

maisumakun

2020/10/26 11:52

過去質問と同じ問題のような気がするのですが…
yozakura10

2020/10/26 12:14

すいません、非ログイン時のことで前の質問を見返してもわからなかったです。 よろしければ教えてくださると助かります
maisumakun

2020/10/26 12:17

user_path(current_user)をログアウト時に使うのが適当ではない、というのは共通です。
yozakura10

2020/10/26 12:24

user_path(@prototype.user)にするのが合っていますか
yozakura10

2020/10/26 12:27

app/views/prototypes/index.html.erbのuser_path(current_user) をuser_path(@prototype.user)にすると添付した画面から 画像が消えてしまいます。
maisumakun

2020/10/26 12:46

えっと、画像はどこで表示しようとしているのですか?
yozakura10

2020/10/26 13:18

indexの画面で非ログインでもログイン時でもprotitypeごとに 画像があるのでそれを今は表示させています。_prototypeを追加しました ログイン時だとタイトルを押すと詳細ページに移動できたのですが 非ログインだと上記のエラー文が出ました。app/views/prototypes/index.html.erbの7行目のuser_path(current_user) をuser_path(@prototype.user)を変えたらindexの画面の画像が消えました
guest

回答1

0

自己解決

app/views/prototypes/show.html.erb
<%= comment.comment %>を<%= comment.text %>に直した

投稿2020/10/27 05:53

yozakura10

総合スコア8

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問