Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on rails - paperclip not saving to database
    primarykey
    data
    text
    <p>I'm trying to make a create product page in rails. This includes adding multiple images. I have one model for products one for photos and for users. I'm using the paperclip gem for photo upload. But I have 2 problems.</p> <ol> <li>My file input is <strong><em>not</em></strong> allowing me to select multiple images</li> <li>When I view a product no pictures show because pictures <strong><em>are not being saved to database</em></strong></li> </ol> <p>P.S. I use HAML and I dont have a photo controller.</p> <p><strong>Products controller</strong></p> <pre><code>class ProductsController &lt; ApplicationController before_filter :current_user, only: [:create, :destory] before_filter :correct_user, only: :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 =&gt; "Sale created!" else render "new", :notice =&gt; "Somehting went wrong!" end end def show @product = Product.find(params[:id]) end </code></pre> <p><strong>create product page</strong></p> <pre><code>= form_for @product, :html =&gt; { :multipart =&gt; 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 = fields_for :photos do |f_i| =f_i.file_field :image %p.button = f.submit </code></pre> <p><strong>product model</strong></p> <pre><code>class Product &lt; 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 </code></pre> <p><strong>photo model</strong></p> <pre><code>class Photo &lt; ActiveRecord::Base attr_accessible :product_id belongs_to :product has_attached_file :image, :styles =&gt; { :thumb=&gt; "100x100#", :small =&gt; "300x300&gt;", :large =&gt; "600x600&gt;" } end </code></pre> <p><strong>User Model</strong></p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :email, :password, :password_confirmation, :name attr_accessor :password has_many :products, dependent: :destroy has_many :photos,:through=&gt;:products </code></pre> <p><strong>show product page</strong></p> <pre><code> %b seller = @product.user.name %br - @product.photos.each do |photo| = image_tag photo.image.url </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload