0

我在 Ruby on Rails 中收到以下错误undefined method 'each' for 0:Fixnum

这是应用程序跟踪:

app/controllers/videos_controller.rb:23:in `new'
app/controllers/videos_controller.rb:23:in `create'

我的控制器创建和新操作:

  def new
    @video = Video.new
  end

  def create
    method = 'get_' + params[:video][:provider] + '_video_id'
    params[:video][:provider_video_id] = Video.send(method, params[:video][:url])
    params[:video][:thumb] = Video.get_thumb_from_youtube(params[:video][:provider_video_id])
    params[:video][:views] = params[:video][:likes] = 0    
    params[:video][:user_id] = current_user
    @video = Video.new(params[:video])
    if @video.save
      redirect_to video_path(@video), notice:'Video added successfully.'
    else
      render :new
    end
  end

这是我的 view.html.haml :

= form_for @video do |f|
 - if @video.errors.any?
  .error_explanation
    %h2= pluralize(@video.errors.count, "error")
    prohibited this user from being saved:

    %ul
      - @video.errors.full_messages.each do |msg|
        %li= msg
.field
  = f.label :title
  = f.text_field :title
.field
  = f.label :description
  = f.text_area :description
%br
.field
  = f.label :url, 'URL'
  = f.text_field :url
%br
.field
  Provider
  = radio_button :video, :provider, 'vimeo'
  = f.label :provider, 'Vimeo', :value => 'vimeo'
  = radio_button :video, :provider, 'youtube' 
  = f.label :provider, 'Youtube', :value => 'youtube'
%br
.field
  Category
  = collection_select(:video, :category_id, Category.all, :id, :name, :include_blank => true)
%br
.actions
  = f.submit "Add video"   
4

1 回答 1

2

params[:video][:views] = params[:video][:likes] = 0

params[:video][:views] = 0

我假设这video.likes是一个关联,而不是计数,所以它需要一个Enumerable. 如果是关联,rails 会尝试将您添加到模型的元素分配给likes模型video。添加它们的第一步是迭代使用each. 这就是错误的来源。

于 2012-07-14T18:36:02.337 回答