Rails no implicit conversion of Symbol into Integer

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

I am trying to create a private method that does some counting based on some database data as follows.

class ApplicationController < ActionController::Base   # Prevent CSRF attacks by raising an exception.   # For APIs, you may want to use :null_session instead.   protect_from_forgery with: :exception      before_action :moderation_count      private      def moderation_count       @count = '';       @count[:venues_pending] = Venue.where(:is_approved => 0).count.to_i       @count[:venues_rejected] = Venue.where(:is_approved => 2).count.to_i       @count[:venue_photos_pending] = VenuePhoto.where(:is_approved => 0).count.to_i       @count[:venue_photos_rejected] = VenuePhoto.where(:is_approved => 2).count.to_i       @count[:venue_reviews_pending] = VenueReview.where(:is_approved => 0).count.to_i       @count[:venue_reviews_rejected] = VenueReview.where(:is_approved => 2).count.to_i     end end 

Error:

no implicit conversion of Symbol into Integer

回答1:

You have set @count as an empty String with

@count = ''; 

so when you do @count[:venues_pending], Ruby tries to convert the Symbol :venues_pending to an Integer to access a particular index of the String @count. This is resulting in the error as no implicit conversion of Symbol into Integer

As you are planning to use the instance variable @count as a Hash, you should instantiate it as a Hash rather than an empty String.

Use @count = {}; or @count = Hash.new;



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!