Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple Uploads Rails Paperclip not Saving
    primarykey
    data
    text
    <p>I have a rails model 'Guitar' and a rails model 'Photo.' I wanted to be able to attach multiple photos so I watched the railscasts for Paperclip, read the docs, etc. I've used Paperclip in the past to attach a single photo, but I wanted to do multiples this time. I don't have a problem updating the model, and it doesn't error out when I attach sample photos, but when I rails console to Photos.all, nothing comes back. My ultimate goal is to a polymorphic association with an 'Amps' model, but I really just wanted to get 'Guitars' working first.</p> <p>I have paperclip added to the Gemfile and I've bundle installed, restarted the server, etc. I think I'm missing something stupid and I'm new to rails so please be gentle...</p> <p>guitar.rb</p> <pre><code>class Guitar &lt; ActiveRecord::Base belongs_to :user has_many :photos accepts_nested_attributes_for :photos end </code></pre> <p>photo.rb</p> <pre><code>class Photo &lt; ActiveRecord::Base belongs_to :guitars has_attached_file :photo, styles: { thumb: '100x100&gt;', square: '200x200#', medium: '300x300&gt;', large: '600x600#' } end </code></pre> <p>guitars_controller.rb</p> <pre><code>class GuitarsController &lt; ApplicationController before_action :set_guitar, only: [:show, :edit, :update, :destroy] # GET /guitars # GET /guitars.json def index @guitars = Guitar.all end # GET /guitars/1 # GET /guitars/1.json def show end # GET /guitars/new def new @guitar = Guitar.new 3.times { @guitar.photos.build } end # GET /guitars/1/edit def edit @guitar = Guitar.find(params[:id]) 3.times { @guitar.photos.build } end # POST /guitars # POST /guitars.json def create @guitar = Guitar.new(guitar_params) @guitar.user_id = current_user.id if current_user respond_to do |format| if @guitar.save format.html { redirect_to @guitar, notice: 'Guitar was successfully created.' } format.json { render action: 'show', status: :created, location: @guitar } else format.html { render action: 'new' } format.json { render json: @guitar.errors, status: :unprocessable_entity } end end end # PATCH/PUT /guitars/1 # PATCH/PUT /guitars/1.json def update respond_to do |format| if @guitar.update(guitar_params) format.html { redirect_to @guitar, notice: 'Guitar was successfully updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @guitar.errors, status: :unprocessable_entity } end end end # DELETE /guitars/1 # DELETE /guitars/1.json def destroy @guitar.destroy respond_to do |format| format.html { redirect_to guitars_url } format.json { head :no_content } end end private # Use callbacks to share common setup or constraints between actions. def set_guitar @guitar = Guitar.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def guitar_params params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :one_owner, :user_id) end end </code></pre> <p>guitars show.html.erb</p> <pre><code>&lt;p&gt; &lt;strong&gt;Make:&lt;/strong&gt; &lt;%= @guitar.make %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Model:&lt;/strong&gt; &lt;%= @guitar.model %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Year:&lt;/strong&gt; &lt;%= @guitar.year %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Color:&lt;/strong&gt; &lt;%= @guitar.color %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Serial:&lt;/strong&gt; &lt;%= @guitar.serial %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Price:&lt;/strong&gt; &lt;%= @guitar.price %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Condition:&lt;/strong&gt; &lt;%= @guitar.condition %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Kind:&lt;/strong&gt; &lt;%= @guitar.kind %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Bodykind:&lt;/strong&gt; &lt;%= @guitar.bodykind %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;Frets:&lt;/strong&gt; &lt;%= @guitar.frets %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;One owner:&lt;/strong&gt; &lt;%= @guitar.one_owner %&gt; &lt;/p&gt; &lt;p&gt; &lt;strong&gt;User:&lt;/strong&gt; &lt;%= @guitar.user_id %&gt; &lt;/p&gt; &lt;div class="thumb"&gt; &lt;% for asset in @guitar.photos %&gt; &lt;%= link_to image_tag(photo.photos.url(:thumb)), photo.photo.url(:original) %&gt; &lt;% end %&gt; &lt;/div&gt; &lt;%= link_to 'Edit', edit_guitar_path(@guitar) %&gt; | &lt;%= link_to 'Back', guitars_path %&gt; </code></pre> <p>guitars _form.html.erb</p> <pre><code>&lt;%= simple_form_for(@guitar, html: { class: 'form-horizontal', :multipart =&gt; true}) do |f| %&gt; &lt;% if @guitar.errors.any? %&gt; &lt;div id="error_explanation"&gt; &lt;h2&gt;&lt;%= pluralize(@guitar.errors.count, "error") %&gt; prohibited this guitar from being saved:&lt;/h2&gt; &lt;ul&gt; &lt;% @guitar.errors.full_messages.each do |msg| %&gt; &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;%= f.input :make, label: 'Make of the Guitar', placeholder: 'Gibson' %&gt; &lt;%= f.input :model, label: 'Model of the Guitar', placeholder: 'Les Paul' %&gt; &lt;%= f.input :year, label: 'Year Made', placeholder: '1957' %&gt; &lt;%= f.input :color, label: 'Color', placeholder: 'GoldTop' %&gt; &lt;%= f.input :serial, label: 'Serial', placeholder: '#7-8789' %&gt; &lt;%= f.input :price, label: 'Price' %&gt; &lt;%= f.input :condition, label: 'Condition, 1-10' %&gt; &lt;%= f.input :kind, label: 'Kind of Guitar', placeholder: '6-String-Electric' %&gt; &lt;%= f.input :bodykind, label: 'Body Type', placeholder: 'Solid String Electric' %&gt; &lt;%= f.input :frets, label: 'Number of Frets', placeholder: '22' %&gt; &lt;%= f.input :one_owner, label: 'Original Owner' %&gt; &lt;%= f.fields_for :photos do |photo| %&gt; &lt;% if photo.object.new_record? %&gt; &lt;%= photo.file_field :photo %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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