rubyruby-on-rails-3routeslink-to

Trying to get reviews for a specfic game


Im attempting to see reviews for a specific game that ive created. Right now my reviews/index show for all games, not just the specific game im looking for. Im assuming its just something im missing in my params but not sure...

these my are controllers...

class ReviewsController < ApplicationController
  before_action :set_review, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except:[:index, :show]
  before_action :correct_user, only: [:edit, :update, :destroy]

  def index
    @reviews = Review.all
  end

  def show
    @review = Review.find(params[:id])
  end 
...
class GamesController < ApplicationController
  before_action :set_game, only: [:show, :edit, :update, :destroy]
 
  def index
    @games = Game.all
  end


  def show
  end

my routes ...

reviews#index
                                      POST   /games/:game_id/reviews(.:format)                                                        reviews#create
                      new_game_review GET    /games/:game_id/reviews/new(.:format)                                                    reviews#new
                     edit_game_review GET    /games/:game_id/reviews/:id/edit(.:format)                                               reviews#edit
                          game_review GET    /games/:game_id/reviews/:id(.:format)                                                    reviews#show
                                      PATCH  /games/:game_id/reviews/:id(.:format)                                                    reviews#update
                                      PUT    /games/:game_id/reviews/:id(.:format)                                                    reviews#update
                                      DELETE /games/:game_id/reviews/:id(.:format)                                                    reviews#destroy
                                games GET    /games(.:format)                                                                         games#index
                                      POST   /games(.:format)                                                                         games#create
                             new_game GET    /games/new(.:format)                                                                     games#new
                            edit_game GET    /games/:id/edit(.:format)                                                                games#edit
                                 game GET    /games/:id(.:format)                                                                     games#show
                                      PATCH  /games/:id(.:format)                                                                     games#update
                                      PUT    /games/:id(.:format)                                                                     games#update
                                      DELETE /games/:id(.:format)                                                                     games#destroy
                                 root GET    /                                                                                        games#index

this is how im attempting to do my link_to but its showing all the games reviews in my seeds not just a single game and all its reviews...

<%= link_to 'reviews', game_reviews_path(game_id: @game.id), @reviews %>

first RoR prj on my own, any help would be great!!

thanks!


Solution

  • The reason is not working is because you are not using the game_id you are passing to the ReviewsController. You need to query for Reviews belonging to the specific Game.

    def index
      @reviews = Review.where(game_id: params[:game_id]).all
    end