Rails - 回形针 - 多张照片上传不保存 [英] Rails - paperclip - Multiple photo upload not saving

查看:51
本文介绍了Rails - 回形针 - 多张照片上传不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a create product page in rails. This includes adding multiple images and text fields. I have one model for products and one for photos. I'm using the paperclip gem for photo upload. But I get no picture when I view product page. Photos are not being saved to database.

附言我使用 HAML.

P.S. I use 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 

推荐答案

您在照片模式下写成:

has_many :photos, :through => :products

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

This doesn't make any sense.. in your Photo model write

belongs_to :product

在产品模型中写入:

has_many :photos

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

actually this will be one-to-many relation as far i had understood:

产品模型中不需要写has_attached_file.这将在照片模型下写入

There is no need to write has_attached_file in product model. This will be written under photo model like

has_attached_file :image

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

Now you have multiple photos for product. So you need loop for showing those photos. e.g., in your show view do some thing

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

_______ 编辑 2 ________

_______ EDIT 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

in your 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 

立即试用.!

这篇关于Rails - 回形针 - 多张照片上传不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆