0

I used

rails: 4.2.9
ruby: 2.3.1p112

I created uses, attractions, and attraction_photos model. I want every user has much attraction and each attraction has many attractions photo. Attraction create is work, but attraction_photos is empty, and the file doesn't upload success.

Gemfile

gem 'carrierwave'
gem 'mini_magick'

attraction photos migrate

class CreateAttractionPhotos < ActiveRecord::Migration
  def change
    create_table :attraction_photos do |t|
      t.integer :attraction_id
      t.string :photo

      t.timestamps null: false
    end
  end
end

User

class User < ActiveRecord::Base

    has_many :attractions
    ...
end

Attraction

class Attraction < ActiveRecord::Base
  belongs_to :user
  has_many :attraction_photos, dependent: :destroy

  accepts_nested_attributes_for :attraction_photos
  ...
end

Attraction_photo

class AttractionPhoto < ActiveRecord::Base
  belongs_to :attraction

  mount_uploader :photo, AttractionImageUploader
end

AttractionImageUploader

class AttractionImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::MiniMagick
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  process resize_to_fit: [800, 800] 

  version :thumb do
    process resize_to_fit: [200, 200]
  end

  version :medium do 
    process resize_to_fill: [400, 400]
  end

  def extension_whitelist
    %w(jpg jpeg gif png)
  end
end

attractions_controller

  def new
    @attractions = Attraction.new
    @attraction_photos = @attractions.attraction_photos.new
  end

  def create
    @attractions = current_user.attractions.build(attraction_params)

    if @attractions.save
      flash[:success] = "success"
      redirect_to attractions_path
    else
      flash[:danger] = @attractions.errors.full_messages
      render action: :new
    end
  end

  private

  def attraction_params
    params.require(:attraction).permit(:name, :longitude, :latitude, :address, :phone, :category_id, attraction_photos: [:photo])
  end

attraction new.html.erb

<%= form_for(@attractions) do |f| %>
    <div class="form-group">
      <%= f.label :name, t("attraction.name"), class: "col-md-2 control-label" %>
      <div class="col-md-10">
        <%= f.text_field :name, class: "form-control" %>
      </div>
    </div>

    <div class="form-group">
      <%= f.label :attraction_photos, t("attraction.attraction_photo"), class: "col-md-2 control-label" %>
      <div class="col-md-10">
        <%= f.file_field :photo, class: "form-control" %>
      </div>
    </div>
lighter
  • 2,808
  • 3
  • 40
  • 59

1 Answers1

0

I recomend you to use https://github.com/nathanvda/cocoon

But if you don't want to use gems:

Like this:

<%= f.fields_for :attraction_photos do |att_photo| %>
    <%= att_photo.label :photo %>
    <%= att_photo.file_field :photo %>
<% end %>
  • Remember to build attraction photos in the new controller's action. @attraction.attraction_photos.build

  • You should fix permitted parameters from attraction_photos: [:photo] to attraction_photos_attributes: [:id, :photo]

kolas
  • 754
  • 5
  • 16
  • `<%= f.fields_for :attraction_photos` is equal my attraction_controller's `@attraction_photos`? – lighter Oct 25 '17 at 07:18
  • @lighter no, its get objects from association ```attraction_photos``` of parent form's object ```@attraction``` – kolas Oct 25 '17 at 08:15
  • I tried to modify upload multiple files, 1. I add this to my view multiple: true` . 2. modify permitted `attraction_photos_attributes: [[:id, :photo] => []]` Can I auto-generate multiple photo row data? – lighter Oct 25 '17 at 09:26
  • @lighter you should build attraction_photos two or more times in the controller – kolas Oct 25 '17 at 10:55