gpt4 book ai didi

javascript - 充当嵌套资源的 Votable

转载 作者:行者123 更新时间:2023-12-03 05:04:46 25 4
gpt4 key购买 nike

我是 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

最后这是我的错误

enter image description here

请像向新手或 5 岁 child 那样解释。

谢谢Eshan,按照你的回答,得到了以下错误。

enter image description here

最佳答案

  1. 作为新手,你表现得很好。

  2. 当您遇到与路由相关的错误时,您要做的第一件事是运行 rails routes在命令行中,这样你就可以看到你面前的路线:

enter image description here

问题:

您遇到 listing_review_user_upvote 的问题路径,通向 /listings/:listing_id/reviews/:review_id/user/:user_id/upvote(.:format)如上所示的 URI 模式。

该路由需要三个参数:listing_idreview_iduser_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 ,您不需要将其与自定义路由一起使用,例如: upvotedownvote .

更新:

  • 您应该在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com