2016-05-01 9 views
0

Ich arbeite an einer Schiene App w/Standorte, Fitnessstudios, Benutzer und Bilder. All diese Bilder teilen sich, so dass ich anstelle des Bilduploads getrennt nach Modell die Lösung, die here ausgedrückt wurde, mochte und sie neu erstellen wollte.Rails Datei hochladen w/Paperclip und polymorphe Assoziationen

Gerade jetzt sehen meine Modelle wie dieses

class Picture < ActiveRecord::Base 
    belongs_to :imageable, polymorphic: true 
    has_attached_file :image, style: { small: '64x64', medium: '100x100', large: '200x200' } 
    validates_attachment :image, presence: true, content_type: { content_type: /\Aimage\/.*\Z/ }, 
               size: { in: 0..5.megabytes } 
end 

class Location < ActiveRecord::Base 
    geocoded_by :county_name 
    after_validation :geocode 

    has_many :pictures, as: :imageable 
    accepts_nested_attributes_for :pictures #added, not sure if needed or not 
end 

und hier ist die Standorte Controller. Meine größte Sorge mit der Steuerung ist, wenn ich die verschachtelte params

class Admin::LocationsController < ApplicationController 
    before_action :authorize_admin! 

    def new 
    @location = Location.new 
    @location.pictures.build 
    end 

    def create 
    @location = Location.new(location_params) 
    if @location.save 
     flash[:success] = 'Location created!' 
     redirect_to admin_location_path(@location) 
    else 
     render 'new' 
    end 
    end 
    ... 
    private 
    def location_params 
    params.require(:location).permit(:county_name, :description, 
           picture_attributes: [:location_id, :id, :image]) 
    end 
end 

und meine Ansicht nach

<section class="new-product"> 
    <div class="container"> 
    <div class="row white_panel"> 
     <div class="col-lg-12"> 
       <h2 class="text-center">Add New Location</h2> 
       <hr> 
       <%= form_for([:admin, Location.new], html: { multipart: true, class: 'form-horizontal' }) do |f| %> 
        <%= render 'shared/error_messages', object: f.object %> 
        <div class="form-group"> 
         <%= f.label :county_name %><br> 
         <%= f.text_field :county_name, class: 'form-control' %><br> 
        </div> 
        <div class="form-group"> 
         <%= f.label :description %><br> 
         <%= f.text_area :description, class: 'form-control' %><br> 
        </div> 
        <p>Upload Picture</p> 
        <%= f.fields_for :picture, Picture.new do |image_upload| %> 
        <%= image_upload.file_field :image, style: 'padding-bottom: 25px;' %> 
        <% end %> 
        <%= f.submit 'Go!', class: 'btn btn-gen np' %> 
       <% end %> 
      </div> 
     </div> 
    </div> 
</section> 

jetzt Handhabung, ich bin keine Fehler bekommen. Die Lage ist gut gemacht, aber nichts passiert auf dem Bildertisch. Es gibt nur nichts zurück.

Kann jemand sehen, was ich damit falsch mache? Ich habe einige Ressourcen dafür gesehen, aber es scheint, als ob es normalerweise mit alten Versionen ist.

Wenn es jemand hilft Ich verwende
rails 4.2.6
ruby 2.2.2p95
paperclip 4.3.6

jede Hilfe überhaupt darüber, wie diese Beziehungen sehr geschätzt würde zu handhaben sein :)

Antwort

0

konnte ich Finde das heraus dank this

view

<%= form_for([:admin, Location.new], html: { multipart: true, class: 'form-horizontal' }) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="form-group"> 
     <%= f.label :county_name %><br> 
     <%= f.text_field :county_name, class: 'form-control' %><br> 
    </div> 
    <div class="form-group"> 
     <%= f.label :description %><br> 
     <%= f.text_area :description, class: 'form-control' %><br> 
    </div> 
    <p>Upload Pictures</p> 
     <%= file_field_tag "images[]", type: :file, multiple: true, style: 'padding-bottom: 25px;' %> 
    <%= f.submit 'Go!', class: 'btn btn-gen np' %> 
<% end %> 

Controller

class Admin::LocationsController < ApplicationController 
    before_filter :authenticate_admin! 

    def new 
    @location = Location.new 
    @location.pictures.build # I don't know if this is necessary or not 
    end 

    def create 
    @location = Location.new(location_params) 
    if @location.save 
     # images 
     if params[:images] 
     params[:images].each do |image| 
      @location.pictures.create(image: image, imageable_id: @location.id) 
     end 
     end 
     flash[:success] = 'Location created!' 
     redirect_to admin_location_path(@location) 
    else 
     render 'new' 
    end 
    end 
    ... 
    private 
    def location_params 
    # took out the other attr's 
    params.require(:location).permit(:county_name, :description) 
    end 
end 

jetzt kann ich
Picture.find(1).image.url oder @locations.pictures.first.image.url und alles funktioniert gut :)