閲覧いただきありがとうございます。
チュートリアルを見ながら、レストランの口コミサイト(食べログのような)作成に挑戦しています。
チュートリアルで「Micropost」とされているところを「Review」に変更して取り組んでいるのですが、紐付けがうまくいっていないのか苦戦しています。とりわけ問題となっているのが、レストランそれぞれに地域カテゴリをつけて表示させているので、初心者の私は完全にルーティングで混乱状態です(涙)
実現したいこと
・レストランページ(category/tokyo/restaurants/1)上に、Userモデルに紐付いた口コミ(review)を表示させたい
エラー内容
NoMethodError in Spaces#review
Ruby
1<%if @feed_items.any? %> 2 <%= render @feed_items %> 3 <%= will_paginate @feed_items %> 4<% end %>
Trace of template inclusion: app/views/categories/tokyo/restaurants/1.html.erb
関連がありそうなファイル
■ ビュー
app/views/restaurants/1.html.erb
Ruby
1・・・ 2<section> 3 <%= render 'shared/feed' %> 4</section> 5・・・
■ モデル
app/models/category.rb
Ruby
1class Category < ApplicationRecord 2 has_many :restaurants 3end
app/models/review.rb
Ruby
1class Review < ApplicationRecord 2 belongs_to :user 3 belongs_to :restaurant 4・・・ 5end
app/model/restaurant.rb
Ruby
1class Restarurant < ApplicationRecord 2 has_many :reviews 3 belongs_to :category 4 5・・・ 6 7 def feed 8 Review.where("user_id = ?", id) 9 end 10end
app/model/user.rb
Ruby
1 has_many :reviews, dependent: :destroy 2・・・
■ コントローラー
app/controllers/categories_controller.rb
Ruby
1class CategoriesController < ApplicationController 2 def show 3 @category = Category.find_by(category_name: params[:category_name]) 4 end 5end
app/controllers/reviews_controller.rb
Ruby
1class ReviewsController < ApplicationController 2 before_action :logged_in_user 3 before_action :correct_user, only: :destroy 4 5 def create 6 @review = current_user.reviews.build(review_params) 7 if @review.save 8 flash[:success] = "口コミが投稿されました!" 9 redirect_to root_path 10 else 11 @feed_items = [] 12 redirect_to root_path 13 end 14 end 15 16 def new 17 @review = current_user.reviews.build if logged_in? 18 end 19 20 def destroy 21 @review.destroy 22 flash[:success] = "口コミが削除されました" 23 redirect_to request.referrer || root_url 24 end 25 26 def edit 27 end 28 29 def update 30 end 31 32 private 33 34 def review_params 35 params.require(:review).permit(:content) 36 end 37 38 def correct_user 39 @review = current_user.reviews.find_by(id: params[:id]) 40 redirect_to root_url if @review.nil? 41 end 42end
app/controllers/restaurants_controller.rb
Ruby
1class RestaurantsController < ApplicationController 2 def show 3 if logged_in? 4 @review = current_user.reviews.build 5 # @feed_items = current_user.feed.paginate(page: params[:page]) 6 end 7 end 8・・・
app/controllers/users_controller.rb
Ruby
1class UsersController < ApplicationController 2 def show 3 @user = User.find(params[:id]) 4 @reviews = @user.reviews.paginate(page: params[:page]) 5 end 6・・・
■ ルーティング
routes.rb
Ruby
1Rails.application.routes.draw do 2 get 'areas/show' 3・・・ 4 resources :areas, param: :category_name do 5 resources :restaurants, only: [:show] 6 end 7・・・ 8 resources :reviews
長々とまた、複雑なケースですみません。。
どうぞよろしくお願いいたします。
あなたの回答
tips
プレビュー