Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Multiple "Add another" in picture_controller.new
1 Post
Multiple "Add another" in picture_controller.new

Hi everyone,

I'm developing a picture manager for stores, using a Picture model that has one paperclip attribute (called "file").

Let's say that Pictures have a category, a store, a creator and a creation date (provided by timestamps), and a "paperclip file"

I've been able to generate common CRUD actions, but I'm having trouble in improving the "new" view.

On my application it will be fairly common that users want to upload several (10, 20 or more) pictures containing the exact same information: store, category, etc. The only thing different about them will be the file.

So I'm trying to include an "add another picture" link on that view, so they can do this.

I'm aware that there's a tutorial discussing something similar on railsforum, and I've also browsed the three related railcasts, but unfortunately these don't apply to my issue, since all of them require a "father model-controller" that I don't have.

Any hints on how to do something similar with just one model would be very appreciated.

Here's a simplified migration:

  def self.up
    create_table :pictures do |t|
      t.references :store
      t.references :category
      t.integer :created_by

      #begin paperclip stuff
      t.string :file_file_name
      t.string :file_content_type
      t.string :file_file_size
      t.datetime :file_updated_at

  def self.down
    drop_table :pictures

My (simplified) Picture model looks like this:

class Picture < ActiveRecord::Base
  belongs_to :category
  has_and_belongs_to_many :tags
  belongs_to :store
  belongs_to :created_by , :class_name => 'User', :foreign_key => 'created_by'

  validates_presence_of :report_date, :store_id, :created_by, :category_id

  # begin of paperclip stuff
  has_attached_file :file,
    :styles => {
      :original => ['640x480>', :jpg]
  attr_protected :file_file_name, :file_content_type, :file_size

  validates_attachment_presence :file
  validates_attachment_size :file, :less_than => 3.megabytes
  validates_attachment_content_type :file, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]

The simplified controller is the following:

class PicturesController < ApplicationController
  # calculate data to fill drop downs in forms' selects
  before_filter :fill_selects, :only => [:new, :create, :edit, :update]
  def show, edit, update, destroy
    #standard scaffolding-generated stuff...
  def new
    @picture = current_user.pictures_created.build()
  def create
    @picture = current_user.pictures_created.build(params[:picture])

    if @picture.save
      flash[:notice] = "Successfully created picture."
      redirect_to @picture
      render :action => 'new'
  def fill_selects
    @stores = Store.find(:all, :order => "name ASC")
    @categories = Category.find(:all, :order => "name ASC")

And finally, here's the view I need to modify:


 DependentSelectFormBuilder, :html => { :multipart => true } do |f| %>
Label: true %>
Category: true %>


So I need to add a link that adds another File: entry on the "files" div, and the controller/model logic associated to those. Any ideas?

1 Post
Login to add your message