2013-10-12 52 views
0

我有一個導軌模型'吉他'和導軌模型'照片'。我希望能夠附加多張照片,所以我看了Paperclip的railscast,閱讀文檔等。我過去使用過Paperclip來附加一張照片,但是這次我想做多個照片。我沒有更新模型的問題,並且當我附加示例照片時不會出錯,但是當我將控制檯導航到Photos.all時,沒有任何內容可以返回。我的最終目標是與'Amps'模型形成多態關聯,但我真的只想讓'吉他'先工作。多個上傳導軌回形針不保存

我有回形針加入Gemfile中,我已經捆綁安裝,重新啓動服務器,等我想我失去了一些東西愚蠢的,我是新來的導軌時,請溫柔...

guitar.rb

class Guitar < ActiveRecord::Base 
    belongs_to :user 
    has_many :photos 

    accepts_nested_attributes_for :photos 
end 

photo.rb

class Photo < ActiveRecord::Base 
    belongs_to :guitars 

    has_attached_file :photo, styles: { 
     thumb: '100x100>', 
     square: '200x200#', 
     medium: '300x300>', 
     large: '600x600#' } 
end 

guitars_controller.rb

class GuitarsController < 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 

吉他show.html.erb

<p> 
    <strong>Make:</strong> 
    <%= @guitar.make %> 
</p> 

<p> 
    <strong>Model:</strong> 
    <%= @guitar.model %> 
</p> 

<p> 
    <strong>Year:</strong> 
    <%= @guitar.year %> 
</p> 

<p> 
    <strong>Color:</strong> 
    <%= @guitar.color %> 
</p> 

<p> 
    <strong>Serial:</strong> 
    <%= @guitar.serial %> 
</p> 

<p> 
    <strong>Price:</strong> 
    <%= @guitar.price %> 
</p> 

<p> 
    <strong>Condition:</strong> 
    <%= @guitar.condition %> 
</p> 

<p> 
    <strong>Kind:</strong> 
    <%= @guitar.kind %> 
</p> 

<p> 
    <strong>Bodykind:</strong> 
    <%= @guitar.bodykind %> 
</p> 

<p> 
    <strong>Frets:</strong> 
    <%= @guitar.frets %> 
</p> 

<p> 
    <strong>One owner:</strong> 
    <%= @guitar.one_owner %> 
</p> 

<p> 
    <strong>User:</strong> 
    <%= @guitar.user_id %> 
</p> 

<div class="thumb"> 
    <% for asset in @guitar.photos %> 
     <%= link_to image_tag(photo.photos.url(:thumb)), photo.photo.url(:original) %> 
    <% end %> 
</div> 

<%= link_to 'Edit', edit_guitar_path(@guitar) %> | 
<%= link_to 'Back', guitars_path %> 

吉他_form.html.erb

<%= simple_form_for(@guitar, html: { class: 'form-horizontal', :multipart => true}) do |f| %> 
    <% if @guitar.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@guitar.errors.count, "error") %> prohibited this guitar from being saved:</h2> 

     <ul> 
     <% @guitar.errors.full_messages.each do |msg| %> 
    <li><%= msg %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <%= f.input :make, label: 'Make of the Guitar', placeholder: 'Gibson' %> 
    <%= f.input :model, label: 'Model of the Guitar', placeholder: 'Les Paul' %> 
    <%= f.input :year, label: 'Year Made', placeholder: '1957' %> 
    <%= f.input :color, label: 'Color', placeholder: 'GoldTop' %> 
    <%= f.input :serial, label: 'Serial', placeholder: '#7-8789' %> 
    <%= f.input :price, label: 'Price' %> 
    <%= f.input :condition, label: 'Condition, 1-10' %> 
    <%= f.input :kind, label: 'Kind of Guitar', placeholder: '6-String-Electric' %> 
    <%= f.input :bodykind, label: 'Body Type', placeholder: 'Solid String Electric' %> 
    <%= f.input :frets, label: 'Number of Frets', placeholder: '22' %> 
    <%= f.input :one_owner, label: 'Original Owner' %> 

    <%= f.fields_for :photos do |photo| %> 
    <% if photo.object.new_record? %> 
     <%= photo.file_field :photo %> 
    <% end %> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
+0

你有什麼錯誤嗎?嘗試在保存前添加一箇中止,看看你是否得到這些文件。 –

+0

沒有錯誤。它只是不會將照片保存到模型中 - 我可以控制並執行Photos.all,並且似乎沒有任何內容可以保存。 P2.0.0-p247:001> Photo.all Photo Loading(1.6ms)SELECT「photos」。* FROM「photos」 =># momchenr

+0

我在日誌中注意到了這一點:不允許的參數:photos_attributes – momchenr

回答

0

您正在運行到被阻止照片被保存質量分配的保護。該行添加到您的Guitar模式:在我的情況

attr_accessible :photos_attributes 
+0

'attr_accessible'是從Rails中提取出來的一個寶石。請使用新推薦的參數保護模式(strong_parameters),或者將'protected_attributes'添加到您的Gemfile以使用舊的。 我在軌道4上。 – momchenr

+0

此問題標記爲ror-3。強大的參數在Rails 4中是新的,'attr_accessible'仍然是Rails 3中的正確方法。 –

+0

對不起,我用RoR3標記了問題,意思是點擊RoR4。我在4上。 – momchenr

1
def guitar_params 
    params.require(:guitar).permit(:make, :model, :year, :color, :serial, :price, :condition, :kind, :bodykind, :frets, :one_owner, :user_id, photos_attributes: [:photo]) 
end 
+0

這在Rails 3中不起作用(並且這個問題被標記爲rails 3)。 –

+0

這個傢伙說他使用Rails 4 - >「我在Rails 4上」。由momchenr –

+0

對不起,我標記不正確。我改變了這一點。 – momchenr

0

我需要創建一個文檔。

# config/initializer/paperclip.rb 
require 'paperclip/media_type_spoof_detector' 
module Paperclip 
    class MediaTypeSpoofDetector 
    def spoofed? 
     false 
    end 
    end 
end 

這個文件是必須在config> initializers中創建的。如果您的SO是Windows,您需要在配置/環境/ development.rb加入這一行:

Paperclip.options [:comand_path] = 「/ImageMagick-6.8.9-Q16/」