【问题标题】:Rails - paperclip - Multiple photo upload not savingRails - 回形针 - 多张照片上传不保存
【发布时间】:2013-05-06 05:27:14
【问题描述】:

我正在尝试在 Rails 中创建产品页面。这包括添加多个图像和文本字段。我有一个产品模型和一个照片模型。我正在使用回形针 gem 上传照片。 但是当我查看产品页面时,我没有得到图片。照片未保存到数据库中。

附:我使用 HAML。

app/views/products/show.html.haml

  %b Name
  = @product.name
  %br

  %b Description
  = @product.description

  %br
  - @product.photos.each do |photo|
  = image_tag photo.image.url

app/controllers/products_controller

class ProductsController < ApplicationController
  before_filter :require_login
    before_filter :current_user, only: [:create, :destory]

  def new 
    @product = Product.new
    @photo = Photo.new
    5.times { @product.photos.build }
  end

  def create

  @photo = current_user.photos.build(params[:photo])
  @product = current_user.products.build(params[:product])
    if @product.save
        render "show", :notice => "Sale created!"
    else
        render "new", :notice => "Somehting went wrong!"
    end
end

  def show
    @product = Product.find(params[:id]) 
  end

应用/模型/照片

class Photo < ActiveRecord::Base
  attr_accessible :product_id    
  belongs_to :product
  has_attached_file :image,
    :styles => {
      :thumb=> "100x100#",
      :small  => "300x300>",
      :large => "600x600>"
        }
end

应用/模型/产品

class Product < ActiveRecord::Base
  attr_accessible :description, :name, :price, :condition, :ship_method, :ship_price, :quantity, :photo
  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos
  belongs_to :user
end

用户模型

   class User < ActiveRecord::Base
      attr_accessible :email, :password, :password_confirmation, :name

      attr_accessor :password
      has_many :products, dependent: :destroy
      has_many :photos,:through=>:products

app/products/new.html.haml

= form_for @product, :html => { :multipart => true } do |f|
  %p
    = fields_for :photos do |f_i|
      =f_i.file_field :image 

【问题讨论】:

标签: ruby-on-rails ruby ruby-on-rails-3 paperclip


【解决方案1】:

首先你的表单是错误的,q 用于注册照片使用fields_for,然后你使用fields_for f.object.photos 或使用Photo.new do | g |,你的关系模型中的另一个是错误的has_attached_file 是has_many 照片,has_attached_file 回形针适合在模型中使用,而不是在与其他模型的关系中使用。希望对你有帮助,现在有几张照片的产品,我建议你用宝石茧,我根据你的情况去,https://github.com/nathanvda/cocoon

【讨论】:

  • 这句话不够清楚
【解决方案2】:

我觉得

5.times { @product.photos.build }

应该在#new方法中 因为 .build 方法与 @fields_for 交互

【讨论】:

  • 他和模特的关系搞砸了。
【解决方案3】:
   @product.image requires image to be the attribute of product model means image fields should be in products table.

   @product.image should be @product.photo.image.url

【讨论】:

  • 没用,我最终得到了 nil:NilClass 的未定义方法 `photos'
  • 产品有多张照片。@product.photo 不起作用,我想需要一个循环来显示所有照片!
【解决方案4】:

你在照片模型下写成:

has_many :photos, :through => :products

这没有任何意义.. 在你的照片模型中写

belongs_to :product

在产品模型中写入:

has_many :photos

据我所知,实际上这将是一对多的关系:

产品模型中不需要写has_attached_file。这将写在照片模型下,如

has_attached_file :image

现在您有多张产品照片。所以你需要循环来显示这些照片。例如,在你的显示视图中做一些事情

<% unless @product.photos.blank? %>
  <% @product.photos.each do |photo| %>
    <%= image_tag photo.image.url %>
  <% end %>
<% end %>

_______ 编辑 2 ___ _____

在您的产品模型中

has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
attr_accessible :photos_attributes

在你的照片模型中

belongs_to :product
attr_accessible :product_id

在您的产品控制器中

def new 
  @product = Product.new
  5.times { @product.photos.build }
end

def create
  @product = Product.new(params[:product])
  @product.user_id = current_user.id
  if @product.save
      render "show", :notice => "Sale created!"
  else
      render "new", :notice => "Somehting went wrong!"
  end
end

在你的 new.html.haml 中

= form_for @product, :html => { :multipart => true } do |f|
  - if @product.errors.any?
    .error_messages
      %h2 Form is invalid
      %ul
        - for message in @product.errors.full_messages
          %li
            = message
  %p
    = f.fields_for :photos do |f_i|
      =f_i.file_field :image 

立即尝试。 !

【讨论】:

  • 嗯...奇怪的是他们不是
  • 关于我应该做什么的任何想法
【解决方案5】:

查看您的代码后。 Product 根本没有任何图像属性,这很简单。照片有图片属性。

所以你必须访问产品 -> 照片 -> 图片

在控制器显示动作中执行此操作

@product = Product.find(id)
@photos = @product.photos

在show.html.erb中

-@photos.each do |photo|
= image_tag photo.image.url
-end

对于haml 语法,我最近在使用html.erb 进行项目时的应用程序。如果有任何错误,请纠正haml语法。

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    相关资源
    最近更新 更多