【问题标题】:Rails - paperclip - NoMethodErrorRails - 回形针 - NoMethodError
【发布时间】:2013-05-15 10:41:05
【问题描述】:

我正在尝试在 Rails 中创建产品页面。这包括添加多个图像和文本字段。我有一个产品模型和一个照片模型。我正在使用回形针 gem 上传照片。但是当我尝试创建新产品时出现此错误。附言我使用 HAML。

产品中的 NoMethodError#new

Showing /some_app/app/views/products/new.html.haml where line #33 raised: 

undefined method `photo' for :product:Symbol

Extracted source (around line #33): 

33:     = f.file_field :product.photo, multiple: 'multiple'

products/new.html.haml

%h1 
  create item
= 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.label :name
    = f.text_field :name
  %p
    = f.file_field :product.photo, multiple: 'multiple'
  %p.button

产品控制器

class ProductsController < ApplicationController
  def new
    @product = Product.new
    @photo = Photo.new
  end

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

产品型号

class Product < ActiveRecord::Base
  attr_accessible :description, :name, :price, :condition, :ship_method, :ship_price, :quantity, :photo
  has_attached_file :photo

  belongs_to :user

  validates :user_id, presence: true
  validates :name, presence: true, length:  { minimum: 5 }
end

照片模型

class Photo < ActiveRecord::Base
  attr_accessible :product_id

  belongs_to :product
  has_attached_file :image,
    :styles => {
      :thumb=> "100x100#",
      :small  => "300x300>",
      :large => "600x600>"
        }
end

【问题讨论】:

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


    【解决方案1】:

    语法不正确 - 更改:

    = f.file_field :product.photo, multiple: 'multiple'
    

    收件人:

    = f.file_field :photo, multiple: 'multiple'
    

    【讨论】:

    • 我希望我能在现实生活中遇见你,这样我就可以给你一个拥抱。谢谢!它有效!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多