【问题标题】:nested image model objects not showing on edit action嵌套图像模型对象未在编辑操作中显示
【发布时间】:2013-07-16 03:13:08
【问题描述】:

我已经忽略了这个问题有一段时间了,但我不能再这样了。当我 render 产品的 edit 视图时,我的嵌套图像模型对象 product_images 没有显示。因此,当我尝试渲染 show 视图时,我的应用程序调用了一个错误,指出图像为零。

为什么没有显示我的嵌套产品图片?我正在使用回形针和 s3。

我需要可编辑的嵌套图像表单,创建新产品时一切正常,编辑是唯一的问题。 show action 期间的 nil 异常仅与 edit action 故障有关

下面是我的代码:

型号##

class ProductImage < ActiveRecord::Base
  attr_accessible :product_id, :photo, :image_width, :image_height
  belongs_to :product
  has_attached_file :photo, :styles => { :small => "150x150>"}, :dependent => :destroy

  validates_attachment_presence :photo
  validates_attachment_size :photo, :less_than => 5.megabytes
  validates_attachment_content_type :photo, :content_type => ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
  after_post_process :save_image_dimensions

  def save_image_dimensions
      geo = Paperclip::Geometry.from_file(photo.queued_for_write[:original])
      self.image_width = geo.width.to_i
      self.image_height = geo.height.to_i
  end
  def sized_different?
  if image_width < image_height
          true
  else
          false

  end
  end
end


class Product < ActiveRecord::Base
  attr_accessible :taxable, :shippable, :page_name, :tact_code, :manu_code, :body_code, :name, :alert, :price, :description, :colors_attributes, :sizes_attributes, :extras_attributes, :restraints_attributes, :product_images_attributes
  validates_presence_of :page_name
  validates_presence_of :price
  validates_presence_of :description
  validates_presence_of :name
  has_many :product_images, :dependent => :destroy
  has_many :restraints, :dependent => :destroy
  has_many :colors, :dependent => :destroy
  has_many :sizes, :dependent => :destroy
  has_many :extras, :dependent => :destroy
  accepts_nested_attributes_for :colors, :sizes, :extras, :restraints, :product_images
end

控制器

  def index
          @products = Product.search(params)
      unless current_cart.nil?
          @cart = current_cart
      else
          @cart = nil
      end
  end
  def new
          @product = Product.new
          @product.product_images.build
  end
  def create
          @product = Product.new(params[:product])

          if @product.save
                  render :index
          else
                  render :new
          end
  end
def edit
          @product = Product.find(params[:id])
          @cart = current_cart
  end
  def show
          @product = Product.find(params[:id])
          @cart = current_cart
  end
  def update
          @product = Product.find(params[:id])
          if @product.update_attributes!(params[:product])
            render :index
          else
            render :edit
          end  
   end
end

_表单视图

<%= form_for @adminproduct, validate: true, html:{ multipart: true} do |f| %>
  <div class="span3 field">
    <ul class="unstyled">
      <li><%= f.text_field :name, :placeholder => "Product name" %></li>
      <li><%= f.text_field :manu_code, :placeholder => "Manufacturer code" %></li>
      <li><%= f.text_field :alert, :placeholder => "Alert, example: Save 10%" %></li>
      <li><%= f.text_field :price, :placeholder => "Base price"  %></li>
      <li><%= f.text_area :description, :placeholder => "Product description", :size => "30x10" %></li>

</ul>
  </div>
  <div class="span6 offset1">
    <ul class="unstyled">
      <li><%= f.check_box :taxable %>Item can be taxed</li>
      <li><%= f.check_box :shippable%>Item can be standard shipped</li>

      <li><br /><b>Choose product images:</b></li>
      (the first image will be the product's main image)<br/>
      <%= f.fields_for :product_images do |p|%><%= render 'product_image_fields', f: p %><% end %>
            <li><%= link_to_add_fields "Add image", f, :product_images %></li>

感谢任何见解。

编辑

产品图片部分

<fieldset>
        <%= f.file_field :photo %>
        <%= f.hidden_field :_destroy %>
        <%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>

【问题讨论】:

  • 如果你在编辑和更新之前进入显示页面,它是否有效? product_image_fields 部分是什么样的?如果您使用控制台查看刚刚通过表单创建的对象,它是否正确附加了图像?更新对象时关联会发生什么?
  • 尝试在 Rails 中使用 嵌套属性
  • 使用嵌套属性。该关联在更新时变为 nil,它在创建时起作用。当我查看编辑表单时,有一堆 product_image 字段,但他们都说没有选择文件
  • 您在使用 CarrierWave 吗?

标签: ruby-on-rails forms model photo nested-attributes


【解决方案1】:

在 products_controller 上,修改编辑操作以包含 product_images 以便accepts_nested_attributes_for 知道要更新哪些图像。

def edit
  @product = Product.includes(:product_images).find(params[:id])
  # ...
end

【讨论】:

    【解决方案2】:

    我相信你需要这样调用你的部分:

    <%= render partial: 'product_image_fields', locals: {f: p} %>
    

    如果它不起作用,请在您的原始问题中粘贴您的“product_image_fields”文件和来自控制台的错误消息

    --

    好的,这是你的部分

    <fieldset>
            <%= f.file_field :photo %>
            <%= f.hidden_field :_destroy %>
            <%= link_to "remove", '#', class: "remove_fields" %>
    </fieldset>
    

    您的问题是产品图片上传输入为空。好吧,您使用的是产生input type=file 的file_field,而HTML 不允许您在此类字段中输入值。换句话说:您不能将值从数据库加载到file_field

    这是我的建议:

    将您的部分更改为输出来自数据库的内容。这只是为了验证和确认数据到达正确的地方

    <fieldset>
            <%= f.object.send :photo %>
    
            <%= f.file_field :photo %>
            <%= f.hidden_field :_destroy %>
            <%= link_to "remove", '#', class: "remove_fields" %>
    </fieldset>
    

    您应该在屏幕中显示图像的路径。另请查看 HTML 源代码,Rails 应该使用您的图像记录 ID 添加隐藏字段。

    如果你没问题,那么你需要用 file_field 以外的东西来显示你的图像,尝试使用 image_tag 或任何你想使用的东西

    【讨论】:

    • 我尝试了您的解决方案,但没有成功。我在问题的末尾发布了部分代码。没有错误,编辑时只有空的 product_image 上传输入。
    • 如果我摆脱了 file_field,那么我就不能将这个 _form 部分用于新操作和编辑操作。我想显示缩略图并提供上传更多内容的选项。删除链接也不会破坏对象
    • 我从来没有建议你摆脱file_field!再次阅读,请参阅我建议一种方法来确认数据到达您的表单视图。一旦你确认了这一点,我建议使用image_tag
    【解决方案3】:

    您是否尝试过使用 Rails 提供的美妙的accepts_nested_attributes_for 来处理嵌套属性

    阅读更多http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    编辑 我想最好还是给你一个例子。

    class Product < ActiveRecord::Base
      attr_accessible ..., :product_images_attributes # <======= Pay attention here. Mass Assignment attributes required
      accepts_nested_attributes_for :product_images
      ....
    end
    

    【讨论】:

    • 您在使用 CarrierWave 吗?如果是,那么您应该使用&lt;%= image_tag f.photo_url %&gt; 或类似的东西来显示图像。阅读 Carrierwave gem 文档
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 2019-12-06
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多