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

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

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

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

Ruby

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

Q&A

1回答

1838閲覧

Rails リレーション関係における、カテゴリに紐づいた記事を表示させたい。

salmon_trout

総合スコア7

Ruby on Rails 5

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

Ruby

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

0グッド

0クリップ

投稿2019/08/30 02:27

編集2019/08/30 05:03

●期待する動作
categoriesコントローラのindexアクションから、showアクションに遷移して、投稿時のカテゴリーに結びついた投稿と記事を表示させたい。
indexアクションの表示まではできています。
indexアクション → 各カテゴリーをCategory.allで表示
●困っていること
paramsの受け渡し方がよく理解できていない為、showアクションのビューが表示できていない。
●教えていただきたいこと
①showアクションのparamsの設定の仕方、インスタンスをどのようにDBから引っ張って来ればいいか、②ビューの表示方法を教えていただきたいです。

わかる方いましたら、教えていただきたいです。よろしくおねがいします。イメージ説明

categoriesコントローラ

class CategoriesController < ApplicationController def index @categories = Category.all end def show @category= Category.find(params[:id]) @posts = @category.posts binding.pry end end

categories#index.html.erb

<%= @categories.each do |category|%> <%=link_to category.symptoms, category_path(category.id)%> <%end%>

categories#show.html.erb

<h1>Categories#show</h1> <%=@posts.each do |post| <%= post.title%> <%= post.content%> <%end%>

posts#new.html.erb

<div class="text-center"> <h1>新規投稿画面</h1> </div> <div class-"row"> <div class="col-sm-6 offset-sm-3"> <%= form_with(model: @post, local: true) do |f| %> <div class="form-group"> <%= f.label :title, 'タイトル' %> <%= f.text_field :title, class: 'form-control'%> </div> <div class="form-group"> <%= f.label :content, '内容'%> <%= f.text_area :content, class: 'form-control' %> </div> <div class="field"> <%= f.label :category_id, 'カテゴリ'%> <%= f.collection_select :category_id, Category.all, :id, :symptoms %> </div> <div class="text-center"> <%= f.submit '投稿'%> <%end%> </div> </div> </div>

routing

Rails.application.routes.draw do root to: 'toppages#index' get 'login', to: 'sessions#new' post 'login', to: 'sessions#create' delete 'logout', to: 'sessions#destroy' get 'ranking', to: 'posts#rank' resources :users, only: [:index, :show, :new, :create] resources :categories, only: [:index, :show] resources :posts, only: [:index, :show, :new, :create, :destroy, ] do resource :favorites, only: [:create, :destroy] resources :comments, only: [:create] end end

prefix

categories GET /categories(.:format) categories#index category GET /categories/:id(.:format) categories#show

_navbar.html.erb (パーシャル化してあるナビゲーションバーのcategories#indexへのリンク)

<li class="nav-item"><%= link_to 'カテゴリ', categories_path, class: 'nav-link js-scroll-trigger' %></li>
class Category < ApplicationRecord has_many :posts end
class Post < ApplicationRecord belongs_to :user has_many :comments has_many :favorites, dependent: :destroy belongs_to :category def favorited_by?(user) favorites.where(user_id: user.id).exists? end end
class CreateCategories < ActiveRecord::Migration[5.2] def change create_table :categories do |t| t.string :symptoms t.timestamps end end end
class CreatePosts < ActiveRecord::Migration[5.2] def change create_table :posts do |t| t.string :title t.string :content t.integer :user_id t.integer :category_id t.timestamps end end end

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

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

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

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

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

winterboum

2019/08/30 04:11

提示されたcodeはざっと見た所問題がみえません。 モデルの関連定義の所を見せて下さい
salmon_trout

2019/08/30 04:14

回答していただき、ありがとうございます。 各モデルとマイグレーションファイルは以下のような通りです。 ``` class Category < ApplicationRecord has_many :posts end ``` ``` class Post < ApplicationRecord belongs_to :user has_many :comments has_many :favorites, dependent: :destroy belongs_to :category def favorited_by?(user) favorites.where(user_id: user.id).exists? end end ``` ``` class CreateCategories < ActiveRecord::Migration[5.2] def change create_table :categories do |t| t.string :symptoms t.timestamps end end end ``` ``` class CreatePosts < ActiveRecord::Migration[5.2] def change create_table :posts do |t| t.string :title t.string :content t.integer :user_id t.integer :category_id t.timestamps end end end ```
winterboum

2019/08/30 04:39

コメントでなく、質問の方に書いて欲しかった。。。。 で、 合っている様にみえます。 開こうとしたCategoryにpostがまだ一件もない、ということではないですね?
salmon_trout

2019/08/30 05:05

大変失礼いたしました。 質問の方に追記させていただきました。 そうでしたか、、、 indexビューにshowアクションへのリンクを貼っており、そこから飛ぶと、[504 Gateway Time-out]と言う表記になってしまいます。 これはrailsアプリ以前の問題なのでしょうか?
salmon_trout

2019/08/30 05:06

postにはデータは入っております。
guest

回答1

0

ここは間違えていますね。class Post の
has_many :comments
これは
belongs_to :comment
にして下さい。ただこれが原因ではなさそう。

できている Post のデータの comment_id に値が入っているか念の為見て下さい。

504だとlogに出ているかもしれない、Started GET "/comments/,,,
というような所から 504を返しているところまでlog見せて下さい。

投稿2019/08/30 08:25

winterboum

総合スコア23329

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問