- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Rails 新手,已经创建了一个列表和评论应用程序(yelp 类型),我希望用户对评论进行投票,我已经安装了可投票的行为并配置如下,但我似乎遗漏了一些东西...有什么想法为什么会出现这个错误吗?
下面是我的路线.rb
Rails.application.routes.draw do
devise_for :users
resources :listings do
resources :reviews, except: [:show, :index, :upvote, :downvote] do
resources :user do
put "upvote", to: "reviews#upvote"
put "downvote", to: "reviews#downvote"
end
end
end
get 'pages/about'
get 'pages/how'
get 'pages/faqs'
get 'pages/contact'
get 'pages/privacy'
get 'pages/tos'
get 'pages/guidelines'
root 'listings#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
这是我的show.html.erb,当它起作用时我会把它变成一个部分
<div class="row">
<div class="col-md-3">
<%= image_tag @listing.image_url (:medium) %>
<h3><%= @listing.name %></h3>
<div class="star-rating" data-score= <%= @avg_rating %> ></div>
<p class="small"><%= "#{@reviews.length} reviews" %></p>
<address>
<strong>Address:</strong>
<%= @listing.address %><br>
<strong>Phone:</strong>
<%= @listing.phone %><br>
<strong>Email:</strong>
<%= mail_to @listing.email %> <br>
<strong>Website:</strong>
<%= link_to @listing.website, @listing %>
</address>
<hr>
<p>
<strong>About:</strong>
<p><%= h(@listing.description).gsub(/\n/, '<br/>').html_safe %></p>
</p>
</div>
<div class="col-md-9">
<%= link_to "Haiya, Post a Review ", new_listing_review_path(@listing), class: "btn btn-info" %> <br><br>
<table class="table">
<thead>
<tr>
<th class="col-md-3"></th>
<th class="col-md-9"></th>
</tr>
</thead>
<tbody>
<% if @reviews.blank? %>
<tr>
<p>No reviews yet. Be the first to write one!</p>
<% else %>
<% @reviews.each do |review| %>
<td>
<h4> <%= "#{review.user.first_name.capitalize} #{review.user.last_name.capitalize[0]}." %></h4>
<p class = "small"><%= time_ago_in_words(review.created_at) %> ago </p>
</td>
<td>
<div class="star-rating" data-score= <%= review.rating %> ></div>
<p><%= h(review.comment).gsub(/\n/, '<br/>').html_safe %></p>
<%= link_to 'UP', listing_review_user_upvote_path(@listing, review), method: :put %>
<!-- Check if user is signed in, to enable edit and update -->
<% if user_signed_in? %>
<% if (review.user == current_user) || (current_user.admin?) %>
<%= link_to "Edit", edit_listing_review_path(@listing, review), class: "text-left" %>
<%= link_to "Delete", listing_review_path(@listing, review), method: :delete, class: "text-left" %>
<% end %>
<% end %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
</div>
</div>
<% if user_signed_in? && current_user.admin? %>
<%= link_to 'Edit', edit_listing_path(@listing), class: "btn btn-link" %> |
<% end %>
<%= link_to 'Back', listings_path, class: "btn btn-link" %>
<!--The script for star ratings. -->
<script>
$('.star-rating').raty({
path: 'https://s3.eu-west-2.amazonaws.com/bebuwaphotos/uploads/stars',
readOnly: true,
score: function() {
return $(this).attr('data-score');
}
});
</script>
这是我的评论 Controller
class ReviewsController < ApplicationController
before_action :authenticate_user!
before_action :set_listing
before_action :set_review, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :check_user, only: [:edit, :update, :destroy]
# GET /reviews/new
def new
@review = Review.new
end
# GET /reviews/1/edit
def edit
end
#Added the def upvote and downvote
def upvote
@review = Review.find(params[:id])
@review.upvote_from current_user
redirect_to :back
end
def downvote
@review = Review.find(params[:id])
@review.downvote_from current_user
redirect_to :back
end
# POST /reviews
# POST /reviews.json
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.listing_id = @listing.id
respond_to do |format|
if @review.save
format.html { redirect_to @listing, notice: 'Your review was successfully posted.' }
format.json { render :show, status: :created, location: @review }
else
format.html { render :new }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reviews/1
# PATCH/PUT /reviews/1.json
def update
respond_to do |format|
if @review.update(review_params)
format.html { redirect_to root_url, notice: 'Your Review was successfully updated.' }
format.json { render :show, status: :ok, location: @review }
else
format.html { render :edit }
format.json { render json: @review.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reviews/1
# DELETE /reviews/1.json
def destroy
@review.destroy
respond_to do |format|
format.html { redirect_to listing_path(@listing), notice: 'Your Review was successfully destroyed :(.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_review
@review = Review.find(params[:id])
end
#Check user
def check_user
unless (@review.user == current_user) || (current_user.admin?)
redirect_to root_url, alert: "Sorry, this review belongs to someone else, you can only edit reviews you have posted."
end
end
#set listing
def set_listing
@listing = Listing.find(params[:listing_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def review_params
params.require(:review).permit(:rating, :comment)
end
end
还有我的模型:
class Review < ApplicationRecord
belongs_to :user
belongs_to :listing
validates :rating, :comment, presence: true
validates :rating, numericality: {
only_integer: true,
greater_than_or_equal_to: 1,
less_than_or_equal_to: 5,
message: "You have to select at least 1 star if not more stars.."
}
acts_as_votable
end
最后这是我的错误
请像向新手或 5 岁 child 那样解释。
谢谢Eshan,按照你的回答,得到了以下错误。
最佳答案
作为新手,你表现得很好。
当您遇到与路由相关的错误时,您要做的第一件事是运行 rails routes
在命令行中,这样你就可以看到你面前的路线:
您遇到 listing_review_user_upvote
的问题路径,通向 /listings/:listing_id/reviews/:review_id/user/:user_id/upvote(.:format)
如上所示的 URI 模式。
该路由需要三个参数:listing_id、review_id和user_id,但您在 View 中这样调用它:
listing_review_user_upvote_path(@listing, review)
.
您根本不发送 user_id,同时您甚至不需要它,因为您在 Controller 的操作中使用了 current_user。
我建议您将路线更新为:
resources :listings do
resources :reviews, except: [:show, :index] do
put "upvote", to: "reviews#upvote"
put "downvote", to: "reviews#downvote"
resources :user
end
end
并以这种方式在 View 中使用它,使用 ids 而不是对象:
listing_review_user_upvote_path(@listing.id, review.id)
这两个步骤有望解决您的问题。
最后,except
选项与资源一起使用以避免生成一些 CRUD 路由,例如:update
, index
, edit
,您不需要将其与自定义路由一起使用,例如: upvote
和downvote
.
您应该在 Controller 中使用正确的参数,review_id
而不是id
,更改review = Review.find(params[:id])
至review = Review.find(params[:review_id])
我建议您使用find_by(id: params[:review_id
而不是find(params[:review_id
因为如果 find 方法在数据库中找不到该对象,则会引发异常。
关于javascript - 充当嵌套资源的 Votable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057558/
我有很多这样的 VO 表: http://svo2.cab.inta-csic.es/theory/fps3/fps.php?ID=2MASS/2MASS.H 但我实际上对表格本身并不感兴趣,只对元数
我是 Rails 新手,已经创建了一个列表和评论应用程序(yelp 类型),我希望用户对评论进行投票,我已经安装了可投票的行为并配置如下,但我似乎遗漏了一些东西...有什么想法为什么会出现这个错误吗?
我正在尝试使用 Acts as votable gem 对帖子中的评论实现投票系统。在这个阶段我遇到了这个错误 ActionController::UrlGenerationError in Post
我正在使用一个名为 acts_as_votable 的 gem让我的模型可以投票。 目前一切正常。 但是每次有人为帖子投票时,页面都必须刷新。如何在不刷新页面的情况下进行投票。我可以添加一个简单的 j
您好,我很确定我的问题很愚蠢,但我无法弄清楚它对我的生活有何影响。我有这个家庭作业,它基本上是为了加强我们在类里面学到的关于多态性的知识(顺便说一下,这是 C++)。该程序的基础是一个名为 shape
我是一名优秀的程序员,十分优秀!