NoMethodError - 未定义的方法`file'用于简单查询

时间:2016-01-06 01:08:57

标签: ruby-on-rails ruby-on-rails-4 activerecord

我只是想展示一张图片,但它总是会抛出相同的错误。连接是正确的,但他没有显示图像,而是抛出该错误。

在/ series_images / show和index中,图像显示为

<%= image_tag(@series_image.file, class: "img-responsive",) %>

这没有任何问题。

这是服务器

Started GET "/series/beyond-the-boundary" for 127.0.0.1 at 2016-01-06 01:44:03 +0100
Processing by SeriesController#show as HTML
  Parameters: {"id"=>"beyond-the-boundary"}
  Series Load (0.3ms)  SELECT  "series".* FROM "series" WHERE "series"."slug" = $1  ORDER BY "series"."id" ASC LIMIT 1  [["slug", "beyond-the-boundary"]]
  SeriesImage Load (0.3ms)  SELECT "series_images".* FROM "series_images" WHERE "series_images"."series_id" = $1  [["series_id", 1]]
  Rendered series/show.html.erb within layouts/application (3.4ms)
Completed 500 Internal Server Error in 7ms (ActiveRecord: 0.5ms)

NoMethodError - undefined method `file' for #<SeriesImage::ActiveRecord_Associations_CollectionProxy:0x007fc0e70be6e0>:
  activerecord (4.2.5) lib/active_record/relation/delegation.rb:136:in `method_missing'
  activerecord (4.2.5) lib/active_record/relation/delegation.rb:99:in `method_missing'
  app/views/series/show.html.erb:2:in `_app_views_series_show_html_erb__1653679829410534096_70233272992260'
  actionview (4.2.5) lib/action_view/template.rb:145:in `block in render'
  activesupport (4.2.5) lib/active_support/notifications.rb:166:in `instrument'
  actionview (4.2.5) lib/action_view/template.rb:333:in `instrument'
  actionview (4.2.5) lib/action_view/template.rb:143:in `render'
  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
  actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
  actionview (4.2.5) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
  actionview (4.2.5) lib/action_view/renderer/template_renderer.rb:14:in `render'
  actionview (4.2.5) lib/action_view/renderer/renderer.rb:42:in `render_template'
  actionview (4.2.5) lib/action_view/renderer/renderer.rb:23:in `render'
  actionview (4.2.5) lib/action_view/rendering.rb:100:in `_render_template'
  actionpack (4.2.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'
  actionview (4.2.5) lib/action_view/rendering.rb:83:in `render_to_body'
  actionpack (4.2.5) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
  actionpack (4.2.5) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
  actionpack (4.2.5) lib/abstract_controller/rendering.rb:25:in `render'
  actionpack (4.2.5) lib/action_controller/metal/rendering.rb:16:in `render'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
  activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
  /Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
  activesupport (4.2.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
  activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:43:in `render'
  actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
  actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
  actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.2.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.2.5) lib/active_support/callbacks.rb:117:in `call'
  activesupport (4.2.5) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
  activesupport (4.2.5) lib/active_support/callbacks.rb:505:in `call'
  activesupport (4.2.5) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.5) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.5) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.5) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.5) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.5) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:45:in `serve'
  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.5) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.5) lib/action_dispatch/routing/route_set.rb:817:in `call'
  warden (1.2.4) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.4) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.5) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.5) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.5) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.5) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.5) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
  better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5) lib/rails/engine.rb:518:in `call'
  railties (4.2.5) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
  /Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
  /Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
  /Users/woorari/.rbenv/versions/2.2.3/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'



Started POST "/__better_errors/a376ff2bf7c7dd19/variables" for 127.0.0.1 at 2016-01-06 01:44:03 +0100

调用发生在series / show.html.erb

<%= image_tag(@series.series_image.file, class: "img-responsive",) %>
<p>
  <strong>Name:</strong>
  <%= @series.name %>
</p>

系列控制器

class SeriesController < ApplicationController
  before_action :set_series, only: [:show, :edit, :update, :destroy]

  # GET /series
  # GET /series.json
  def index
    @series = Series.paginate(:page => params[:page], :per_page => 10)
  end

  # GET /series/1
  # GET /series/1.json
  def show
  end

  # GET /series/new
  def new
    @series = Series.new

  end

  # GET /series/1/edit
  def edit
  end

  # POST /series
  # POST /series.json
  def create
    @series = Series.new(series_params)

    respond_to do |format|
      if @series.save
        format.html { redirect_to @series, notice: 'Series was successfully created.' }
        format.json { render :show, status: :created, location: @series }
      else
        format.html { render :new }
        format.json { render json: @series.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /series/1
  # PATCH/PUT /series/1.json
  def update
    respond_to do |format|
      if @series.update(series_params)
        format.html { redirect_to @series, notice: 'Series was successfully updated.' }
        format.json { render :show, status: :ok, location: @series }
      else
        format.html { render :edit }
        format.json { render json: @series.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /series/1
  # DELETE /series/1.json
  def destroy
    @series.destroy
    respond_to do |format|
      format.html { redirect_to series_index_url, notice: 'Series was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_series
      @series = Series.friendly.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def series_params
      params.require(:series).permit(:name, :tagline, :shortDESC, :longDESC, :rating, :release)
    end
end

SeriesImage控制器

class SeriesImagesController < ApplicationController
  before_action :set_series_image, only: [:show, :edit, :update, :destroy]

  # GET /series_images
  # GET /series_images.json
  def index
    @series_images = SeriesImage.paginate(:page => params[:page], :per_page => 5)
  end

  # GET /series_images/1
  # GET /series_images/1.json
  def show
  end

  # GET /series_images/new
  def new
    @series_image = SeriesImage.new
  end

  # GET /series_images/1/edit
  def edit
  end

  # POST /series_images
  # POST /series_images.json
  def create
    @series_image = SeriesImage.new(series_image_params)

    respond_to do |format|
      if @series_image.save
        format.html { redirect_to @series_image, notice: 'Series image was successfully created.' }
        format.json { render :show, status: :created, location: @series_image }
      else
        format.html { render :new }
        format.json { render json: @series_image.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /series_images/1
  # PATCH/PUT /series_images/1.json
  def update
    respond_to do |format|
      if @series_image.update(series_image_params)
        format.html { redirect_to @series_image, notice: 'Series image was successfully updated.' }
        format.json { render :show, status: :ok, location: @series_image }
      else
        format.html { render :edit }
        format.json { render json: @series_image.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /series_images/1
  # DELETE /series_images/1.json
  def destroy
    @series_image.destroy
    respond_to do |format|
      format.html { redirect_to series_images_url, notice: 'Series image was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_series_image
      @series_image = SeriesImage.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def series_image_params
      params.require(:series_image).permit(:series_id, :file, :desc, :head, :headID, :home, :homeID)
    end
end

2个模特

class Series < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, use: :slugged

  validates :name, presence: true

  has_many :series_images

end


class SeriesImage < ActiveRecord::Base
  mount_uploader :file, PictureUploader

  belongs_to :series


end

gemfile

#better IDs
gem 'friendly_id', '~> 5.1'
#Bootstrap
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.6'
#Image Upload
gem 'carrierwave', '~> 0.10.0'
#Bootswatch
gem 'bootswatch-rails', '~> 3.3', '>= 3.3.5'

# Devise User Management
gem 'devise', '~> 3.5', '>= 3.5.3'

#Pagination
gem 'will_paginate', '~> 3.1'
gem 'will_paginate-bootstrap', '~> 1.0', '>= 1.0.1'

Series_Images的迁移

class CreateSeriesImages < ActiveRecord::Migration
  def change
    create_table :series_images do |t|
      t.belongs_to :series, index:true
      t.string :file
      t.string :desc
      t.boolean :head
      t.integer :headID
      t.boolean :home
      t.integer :homeID

      t.timestamps null: false
    end
  end
end

1 个答案:

答案 0 :(得分:0)

问题是你在Series和SeriesImages之间有一对多的关系。因此,当您在视图中调用@series.series_image.first.try(:file)时,您将获得活动记录集合而不是特定图像。您可以尝试执行"Class 'HttpSocket' Not Found" ,这将为您提供第一个series_image(如果存在)。如果失败则返回nil。

系列应该有多个SeriesImages吗?