【问题标题】:Rails - paperclip - multiple image creation no-method errorRails - 回形针 - 多个图像创建无方法错误
【发布时间】:2013-05-25 11:12:08
【问题描述】:

我是 Ruby on Rails 的新手,我一直在尝试创建一个示例苹果,但我一直坚持这一部分 WEEKS ! 我一直在向 stackoverflow 发送垃圾邮件,但我已经没有运气:(。 我正在尝试创建一个还允许上传多个图像的产品页面。所以我有一个用户模型,一个产品模型和一个照片模型。当我提交包含照片和其他输入的表单时,我收到此错误。

NoMethodError in ProductsController#create

undefined method `photo' for #<Product:0x9078f74>

新产品页面

= form_for @product, :html => {:multipart => true} do |f|
  %p
    = f.label :description
    = f.text_field :description

  = fields_for @photo, :html => {:multipart => true} do |fp|
    = fp.file_field :image 

  %p.button
    = f.submit

产品控制器

  def new
    @product = Product.new
    @photo   = Photo.new
  end

  def create
    @photo = current_user.photos.build(params[:photo])  
    @product = current_user.products.build(params[:product])
  end

产品型号

attr_accessible :description, :name, :photo, :image

  belongs_to :user
  has_many :photos, dependent: :destroy
  accepts_nested_attributes_for :photos

  validates :user_id,      presence: true
  validates :description,  presence: true
  validates :photo,        presence: true
end

照片模型

  attr_accessible :image
  belongs_to :product
  validates_attachment :image, presence: true

用户模型

attr_accessible :email, :name, :password, :password_confirmation, :image, :photo

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

end

表格

用户

  • 身份证
  • 姓名
  • 电子邮件
  • 密码

产品

  • 身份证
  • 姓名
  • 说明
  • 用户名

照片

  • 身份证
  • image_file_name
  • image_content_type
  • image_file_size
  • image_updated_at
  • product_id

【问题讨论】:

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


    【解决方案1】:

    在您的产品模型中


    has_many :照片
    accepts_nested_attributes_for :照片
    attr_accessible :description, :name, :photos_attributes

    【讨论】:

      【解决方案2】:

      产品控制器更改为

      def new
        @product = Product.new
        @product.photos.build
      end
      
      def create  
        @product = Product.new(params[:product])
      end
      

      由于您要求上传多张图片,请尝试将其添加到您的视图中

         <%= f.fields_for :photos do |img| %>
              <%= render "img_fields", :f => img %>
         <% end %>
         <div class="add_image"><%= link_to_add_fields "Add Image", f, :photos %></div>
      

      并为视图创建一个文件 _img_fields.html.erb 并添加

      <div class="entry_field">
          <label>Image :</label>
          <%= f.file_field :image %>
          <%= link_to_remove_fields "remove", f %></div>
      

      然后将以下行添加到您的 application.js 文件中

      function remove_fields(link) {
              $(link).prev("input[type=hidden]").val("1");
              $(link).closest(".entry_field").hide();
      }
      
      function add_fields(link, association, content) {
              var new_id = new Date().getTime();
              var regexp = new RegExp("new_" + association, "g");
              $(link).parent().before(content.replace(regexp, new_id));
      }
      

      在您的产品模型中

        has_many :photos
        accepts_nested_attributes_for :photos
        attr_accessible :description, :name, :photos_attributes
      

      【讨论】:

      • 在我更改产品控制器后,该控制器在 Products#new undefined method `model_name' for NilClass:Class Extracted source (about line #18) 中给了我 NoMethodError:18: = fields_for @photo, :html = > {:multipart => true} 做 |fp|
      • 我认为您没有按指定更改视图文件。将视图更改为 如果您只想添加一张图片或上传多张图片,请按照答案中的说明进行操作。
      • #:0xa091b2c> 的未定义方法 `link_to_remove_fields'
      【解决方案3】:

      变化:

      = fields_for @photo, :html => {:multipart => true} do |fp|
      

      收件人:

      = fields_for :photos, :html => {:multipart => true} do |fp|
      

      在你的控制器中:

        def new
          @product = Product.new
          @product.photos.build
        end
      

      在您的产品模型中:

      attr_accessible :description, :name, :photos_attributes
      

      【讨论】:

      • 嗯,我做到了,但它得到了一个有趣的错误。路由错误没有匹配的路由 [post] 但是在我的 config/routes.rb 我有资源 :products 和 resources:photos 所以......谷歌说它与表单中的 :product 有关系
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多