所以我试图创建一个视频游戏评论网站进行练习。
游戏有很多评论和投票。这个想法是,为了发表评论,你必须投票"好"或者"坏"首先,然后提交评论。您无需投票即可发布文字评论。
我试图在没有acts_as_voteable gem的情况下这样做......
投票的数据格式是布尔值。 "良好的"是的,"坏"是假的。
如何获得投票保存?下面是我的routes.rb,_review partial,review controller和show page。
非常感谢你们:)
编辑****:我也试图每个用户只投一票。我正在考虑使用一个等于1的令牌变量,当投票时,令牌为-1。这是一个好方法吗?但是投票的数据类型是布尔值,那么如何工作 - 或者我应该将投票的数据类型从布尔值更改为整数?
编辑#2 - 所以我补充说:投票到我的参数。
的routes.rb
upvote_game_review_path
POST /games/:game_id/reviews/:id/upvote(.:format) reviews#upvote
downvote_game_review_path
POST /games/:game_id/reviews/:id/downvote(.:format) reviews#downvote
Rails.application.routes.draw do
devise_for :users
root "games#index"
resources :games do
resources :news
resources :reviews, except: [:show, :index] do
member do
post "upvote"
post "downvote"
end
end
end
resources :platforms
resources :genres
end
reviews_controller.rb
class ReviewsController < ApplicationController
before_action :set_review, only: [:show, :update, :edit, :destroy]
before_action :set_game
before_action :authenticate_user!
def new
@review = Review.new
end
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.game_id = @game.id
if @review.save
redirect_to @game
else
render "review"
end
end
def upvote
@review.vote.create = true
redirect_to @game
end
def downvote
@review.vote.create
@review.vote = false
redirect_to @game
end
def edit
@review.update(review.params)
end
def destroy
@review.destroy
redirect_to @game
end
private
def set_review
@review = Review.find(params[:id])
end
def set_game
@game = Game.find(params[:game_id])
end
def review_params
params.require(:review).permit(:comment, :vote)
end
end
_review partial&lt; - 创建新评论
<%= form_for [@game, @reviews.new] do |r| %>
<h3 class="post_review">Review this game</h3>
<p>
<%= r.text_area :comment %>
</p>
<p>
<%= button_to "Good", upvote_game_review_path(@game.id, r) %>
</p>
<p>
<%= button_to "Bad", downvote_game_review_path(@game.id, r) %>
</p>
<p>
<%= r.hidden_field :game_id, value: @game.id %>
<p>
<%= r.submit %>
<% end %>
show.html.erb
<p><%= link_to "<< Home", games_path %></p>
<span><%= link_to "Edit", edit_game_path(@game) %></span>
<span><%= link_to "Delete", game_path(@game), method: :delete %></span>
<div class="game_summary">
<h2><%= @game.title %></h2>
<%= image_tag @game.image %>
<p>Release Date: <%= @game.release_date %> </p>
<p>Genre: <%= @game.genre_id %> </p>
<p>Platforms: <%= @game.platform_id %></p>
</div>
<%= link_to "Add News", new_game_news_path(@game) %>
<h2>News & Articles</h2>
<%= link_to "view all", game_news_index_path(@game) %>
<% @news.each do |n| %>
<ol>
<li><%= link_to n.title, game_news_path(@game.id, n.id) %></li>
</ol>
<% end %>
<div class="game_review submit">
<%= render "review" %>
</div>
<% @reviews.each do |review| %>
<p><%= review.comment %></p>
<p><%= link_to "delete", game_review_path(@game.id, review.id), method: :delete %></p>
<% end %>
答案 0 :(得分:0)
您没有指定要加载的评论。原因在于:
before_action :set_review, only: [:show, :update, :edit, :destroy]
当您执行其中任何一项操作时,您都无法提取请求的审核实例。此外,它看起来并非实际保存。
所以,我推荐两件事:
将这些方法添加到before_action
:
before_action :set_review, only: [:show, :update, :edit,
:destroy, :upvote, :downvote]
(可能没有必要,编写测试来确认这一点!)实际上在您更改其值后保存实体。
def upvote
@review.vote.create = true
@review.save
redirect_to @game
end
def downvote
@review.vote.create unless @review.vote
@review.vote = false
@review.save
redirect_to @game
end