简体   繁体   中英

Rails - paperclip - NoMethodError

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 this error when I try to create a new product. PS I use HAML.

NoMethodError in Products#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

Products controller

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

Product model

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

Photo model

class Photo < ActiveRecord::Base
  attr_accessible :product_id

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

The syntax isn't correct - change:

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

To:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM