尝试获取特定游戏的评论
Trying to get reviews for a specfic game
我正在尝试查看对我创建的特定游戏的评论。现在我的 reviews/index 展示了所有游戏,而不仅仅是我正在寻找的特定游戏。我假设它只是我的参数中缺少的东西但不确定...
这些是我的控制器...
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
我的路线...
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
这就是我尝试做我的 link_to 的方式,但它显示了我的种子中的所有游戏评论,而不仅仅是单个游戏及其所有评论...
<%= link_to 'reviews', game_reviews_path(game_id: @game.id), @reviews %>
我自己的第一个 RoR prj,任何帮助都会很棒!!
谢谢!
不起作用的原因是您没有使用传递给 ReviewsController 的 game_id
。您需要查询属于特定游戏的评论。
def index
@reviews = Review.where(game_id: params[:game_id]).all
end
我正在尝试查看对我创建的特定游戏的评论。现在我的 reviews/index 展示了所有游戏,而不仅仅是我正在寻找的特定游戏。我假设它只是我的参数中缺少的东西但不确定...
这些是我的控制器...
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
我的路线...
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
这就是我尝试做我的 link_to 的方式,但它显示了我的种子中的所有游戏评论,而不仅仅是单个游戏及其所有评论...
<%= link_to 'reviews', game_reviews_path(game_id: @game.id), @reviews %>
我自己的第一个 RoR prj,任何帮助都会很棒!!
谢谢!
不起作用的原因是您没有使用传递给 ReviewsController 的 game_id
。您需要查询属于特定游戏的评论。
def index
@reviews = Review.where(game_id: params[:game_id]).all
end