gpt4 book ai didi

ruby-on-rails - TypeError (nil 不能被强制转换为 Float) :

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

我正在尝试实现评级系统。通过 this tutorial

当我尝试给出评级时,我不断收到此错误:

Started PATCH "/ratings/7.json" for 127.0.0.1 at 2014-10-31 14:09:58 +0200
Processing by RatingsController#update as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"j/bCg3mFtQLl1n5qSZInNrUoKaRT/PKpzLnEz6QNGcQ=", "rating"=>{"book_id"=>"1", "user_id"=>"1", "stars"=>"4"}, "id"=>"7"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Rating Load (0.2ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."id" = $1 LIMIT 1 [["id", 7]]
(0.1ms) BEGIN
(0.2ms) COMMIT
Book Load (0.2ms) SELECT "books".* FROM "books" WHERE "books"."id" = $1 LIMIT 1 [["id", 1]]
Rating Load (0.3ms) SELECT "ratings".* FROM "ratings" WHERE "ratings"."book_id" = $1 [["book_id", 1]]
Completed 500 Internal Server Error in 8ms

TypeError (nil can't be coerced into Float):
app/models/book.rb:30:in `+'
app/models/book.rb:30:in `block in avg_rating'
app/models/book.rb:29:in `avg_rating'
app/controllers/ratings_controller.rb:37:in `block (2 levels) in update'
app/controllers/ratings_controller.rb:35:in `update'

我的 show.html.erb View :

<% form_id = "book_#{@book.id}_rating" %>
<% if user_signed_in? %> <!-- To avoid throwing an exception if no user is signed in -->
<% user_id = current_user.id %>
<% else %>
<% user_id = -1 %>
<% end %>
<%= form_for @book.ratings.find_or_create_by(user_id: user_id), :html => {:id => form_id, :class => "star_rating_form"} do |f| %>
<%= f.hidden_field :book_id, :value => @book.id %>
<% if user_signed_in? %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<% end %>
<%= f.hidden_field :stars, :id => form_id + "_stars" %>
<% end %>
<% (1..5).each do |i| %>
<li class="rating_star" id="<%= form_id %>_<%= i %>" data-stars="<%= i %>" data-form-id="<%= form_id %>"></li>
<% end %>

我的 ratings_controller.rb:

  before_filter :js_logged_in

def create
@rating = Rating.new(rating_params)
@book = Book.find(params[:rating][:book_id])

respond_to do |format|
if @rating.save
format.json { render :json => { :avg_rating => @book.avg_rating } }
else
format.json { render :json => @rating.errors, :status => :unprocessable_entity }
end
end
end

def update
@rating = Rating.find(params[:id])

respond_to do |format|
if @rating.update_attributes(rating_params)
format.json { render :json => { :avg_rating => @rating.book.avg_rating } }
else
format.json { render :json => @rating.errors, :status => :unprocessable_entity }
end
end
end


def js_logged_in
if(!user_signed_in?)
flash[:error] = "You must be a signed in to leave a rating!"
render :js => "window.location = '/users/sign_in'"
end
end

private

def rating_params

params.require(:rating).permit(:book_id, :stars, :user_id)

end

我的book.rb模型:

  def avg_rating
average_rating = 0.0
count = 0
ratings.each do |rating|
average_rating += rating.stars
count += 1
end

if count != 0
(average_rating / count)
else
count
end
end

我的 rating.rb 模型:

class Rating < ActiveRecord::Base
belongs_to :book
belongs_to :user
attr_accessor :stars
end

谁知道 - 为什么这不起作用?

最佳答案

您的评级的至少一个 stars 属性返回 nil。因此,当您调用 average_rating += rating.stars 时(相当于 average_rating = average_rating + nil),您会收到错误消息。如果此字段中的 nil 值表示 0,您可能应该:

average_rating += rating.stars || 0

如果此字段中的nil 值不受欢迎,您应该在Rating 模型中对其进行验证:

validates_presence_of :stars

此外,由于 stars 是您的 ratings 表中的一个字段,您不需要 Rating< 中的 attr_accessor :stars/模型。

关于ruby-on-rails - TypeError (nil 不能被强制转换为 Float) :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26675001/

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