Rails + Paperclip - 项目中的 NoMethodError#index - nil:NilClass 的未定义方法“图像”

Rails + Paperclip - NoMethodError in Projects#index - undefined method `image' for nil:NilClass

我有一个带有 Project 模型和嵌套 Pictures 模型的 rails 应用程序。我正在使用 Paperclip gem 将图像上传到 pictures 模型,并使用 nested_form gem 将 Picture 模型嵌套在 projects 中。一切都运行良好,图像正在显示并且代码似乎运行良好,直到在处理应用程序的另一部分后突然,我开始收到问题标题中提到的错误。具体来说,我的索引页面中的这一行似乎是问题所在:<%= link_to image_tag(project.pictures.first.image.url(:thumb)), project %> 我似乎无法弄清楚问题所在,因为它之前工作正常。我什至在它工作时恢复到以前的提交,但我仍然遇到同样的错误。我完全被难住了。任何帮助将不胜感激!

索引:

<div id="pictures">
  <% @projects.each do |project| %>

  <div class="col-md-4">

    <div class="box panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><%= project.title %></h3>
      </div>
      <div class="image">
         <%= link_to image_tag(project.pictures.first.image.url(:thumb)), project %>
      </div>
      <div class="panel-body">

        <p>
          <strong>Progress:</strong>
          <%= progress_bar 0.6, label: true, alternative: 'info', striped: true %>
        </p>


        <p>
          <strong>Status:</strong>
          <%= project.status %>
        </p>


        <p>
          <strong>Phase:</strong>
          <%= project.phase %>
        </p>


        <%= link_to 'Show', project %> |
        <%= link_to 'Edit', edit_project_path(project) %> |
        <%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %>
      </div>
    </div>
      </div>


  <% end %>
</div>

项目模型:

class Project < ActiveRecord::Base

  has_many :pictures, :dependent => :destroy
  has_many :teams, :dependent => :destroy

    accepts_nested_attributes_for :pictures, :reject_if => lambda { |a| a[:image].blank? }, allow_destroy: true
    accepts_nested_attributes_for :teams, :reject_if => lambda { |a| a[:member].blank? }, allow_destroy: true

end

图片型号:

class Picture < ActiveRecord::Base

 belongs_to :project

 has_attached_file :image,
 path: ":rails_root/public/system/:attachment/:id/:style/:filename",
 url: "/system/:attachment/:id/:style/:filename",
:styles => { :medium => "900x900>", :thumb => "300x300>" }

 validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/

end

项目负责人:

class ProjectsController < ApplicationController
  before_action :set_project, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @projects = Project.all
    respond_with(@projects)
  end

  def show
    respond_with(@project)
  end

  def new
    @project = Project.new
    @project.pictures.build
    @project.teams.build
    respond_with(@project)
  end


  def edit
  end

  def create

    @project = Project.new(project_params)
    if @project.save
    flash[:notice] = "Successfully created project."
    redirect_to @project
    else
    render :action => 'new'
    end

  end

  def update
    @project.update(project_params)
    respond_with(@project)
  end

  def destroy
    @project.destroy
    respond_with(@project)
  end

  private
    def set_project
      @project = Project.find(params[:id])
    end

    def project_params
      params.require(:project).permit(:id, :title, :description, :status, :phase, :location, :contractor, :designer, :area, :budget, :project_start, :construction_period, :expected_date, :picture_id, :image, pictures_attributes: [:id, :image, :_destroy], teams_attributes: [:project_id, :user_id, :id, :member, :role, :_destroy])
    end
end

图片控制器:

class PicturesController < ApplicationController
  before_action :set_picture, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def index
    @pictures = Picture.all
    respond_with(@pictures)
  end

  def show
    respond_with(@picture)
  end

  def new
    @picture = Picture.new
    respond_with(@picture)
  end

  def edit
  end

  def create
    @picture = Picture.new(picture_params)
    @picture.save
    respond_with(@picture)
  end

  def update
    @picture.update(picture_params)
    respond_with(@picture)
  end

  def destroy
    @picture.destroy
    respond_with(@picture)
  end

  private
    def set_picture
      @picture = Picture.find(params[:id])
    end

    def picture_params
      params.require(:picture).permit(:image, :id, :project_id)
    end
end

可以肯定的是,您的 projects 之一没有照片。

当您为每个项目调用 project.pictures.first.image.url(:thumb) 时,您将获取其所有图片,然后是其中的第一张图片,然后是第一张图片的图像。

如果你的项目没有图片,那么project.pictures就是一个空集。当您在空集上调用 .first 时,结果为零。 nil 是 class NilClass 的对象,它没有名为 image 的方法。确保您的每个项目都至少有一张图片,这样您就不会看到错误。或者,您可以在 link_to 周围创建一个 if 语句,这样您就只评估该行 if !project.pictures.empty?.