【问题标题】:rails paperclip - trouble uploading multiple images to one model through nested modelrails paperclip - 通过嵌套模型将多个图像上传到一个模型时遇到问题
【发布时间】:2015-07-28 11:32:04
【问题描述】:

一段时间以来,我一直试图弄清楚如何通过嵌套模型将多张图像上传到一个模型,但没有成功。我有一个项目模型,对于每个项目,我想上传多个图像。我创建了一个名为 Picture 的模型并将其嵌套在 Project 模型中,并设置了回形针,一切似乎都很好,除了当我上传图像并单击“创建项目”时,图像不会显示在“显示”页面上。没有显示错误消息。请帮忙,因为我不知道如何从这里开始。

这是我的代码:

项目形式:

<%= bootstrap_nested_form_for @project, :html => {:multipart => true}  do |f| %>

 <% f.fields_for :pictures do |builder| %>
    <% if builder.object.new_record? %>

      <p>
        <%= builder.file_field :image %>
      </p>

    <% end %>
    <%= builder.link_to_remove "Remove" %>
  <% end %>

  <p>
   <%= f.link_to_add "Add Images", :pictures %>
 </p>

 <%= f.submit %>
<% 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
    respond_with(@project)
  end


  def edit
  @project = Project.find(params[:id])
  @project.pictures.build
  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, pictures_attributes: [:id, :image])
    end
end

项目模型:-

class Project < ActiveRecord::Base

  has_many :pictures, :dependent => :destroy
    accepts_nested_attributes_for :pictures, :reject_if => lambda { |t| t['picture'].nil? }

end

图片模型:-

class Picture < ActiveRecord::Base
 belongs_to :project
 has_one :image

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

 validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]

end

显示页面:-

<% @project.pictures do |picture| %>

  <%= image_tag picture.image_url %>
<% end %>

<p>
  <strong>Title:</strong>
  <%= @project.title %>
</p>

<p>
  <strong>Description:</strong>
  <%= @project.description %>
</p>

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

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

<p>
  <strong>Location:</strong>
  <%= @project.location %>
</p>

<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>

架构:-

ActiveRecord::Schema.define(version: 20150728092717) do

create_table "pictures", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "project_id"
  end

  add_index "pictures", ["project_id"], name: "index_pictures_on_project_id"
create_table "projects", force: true do |t|
    t.string   "title"
    t.text     "description"
    t.string   "status"
    t.string   "phase"
    t.string   "location"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

【问题讨论】:

  • 如果创建了图像,那么您将显示错误。这个&lt;%= image_tag picture.image_url %&gt; 应该是&lt;%= image_tag picture.image.url(:medium) %&gt;
  • 试过了,但图像仍然没有显示。有什么想法可能导致该问题吗?

标签: ruby-on-rails paperclip nested-forms


【解决方案1】:

您的表单和白名单使用属性名称image

但如果嵌套图片没有picture 参数,您将拒绝它们。

accepts_nested_attributes_for :pictures, :reject_if => lambda { |t| t['picture'].nil? }

嵌套属性参数没有像rails form params通常那样包装在“模型键”中。这就是他们的样子:

params = {
  project: {
    pictures_attributes: [
      {
        image: 'foo.jpg'
      }
    ]
  }
}

您可以通过模型规格非常简单地发现这些错误:

require 'rails_helper'
RSpec.describe Project do
  it 'accepts nested pictures' do
     project = Project.new(pictures_attributes: [{ image: 'foo.jpg' }])
     expect(project.pictures.first).to to_be_a Picture
  end
end

【讨论】:

  • 我认为你不应该这样做 has_one :imagehas_attached_file :image。我没有使用回形针,但它根本没有意义,因为回形针存储在磁盘上,而不是 ActiveRecord 模型中。
  • 您好,感谢您花时间回答。我仍然不确定如何解决这个问题。我对 Rails 和编码很陌生,所以我真的不知道如何使用模型规格。我应该在哪里实现你提到的 params 代码?
  • 我应该在项目控制器中的 create 操作中添加/更改一些内容吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-11
  • 2011-06-01
  • 1970-01-01
  • 2016-02-02
  • 2023-03-19
  • 1970-01-01
  • 2016-04-20
相关资源
最近更新 更多