0

我有一個屬於圖庫的圖像模型。我在我的畫廊編輯頁面中添加了一個額外的/單獨的表單,以上傳圖像並將它們與圖庫相關聯。我正在使用載波霧來上傳到Rackspace文件。在rails4上使用jquery-fileupload-rails獲取禁止屬性錯誤

我得到的錯誤是禁止的屬性,但它不告訴我哪個屬性/參數導致問題。我怎樣才能進一步調試呢?

模型

class Image < ActiveRecord::Base 
    belongs_to :gallery 

    mount_uploader :file, ImageUploader 

    def to_jq_upload 
    { 
     "name" => read_attribute(:file), 
     "size" => file.size, 
     "url" => file.url, 
     "thumb_url" => file.thumb.url, 
     "delete_url" => image_path(:id => id), 
     "delete_type" => "DELETE" 
    } 
    end 
end 

控制器

class ImagesController < ApplicationController 

    def create 

    p_attr = params[:image] 
    p_attr[:gallery_id] = params[:image][:gallery_id] 
    p_attr[:file] = params[:image][:file].first if params[:image][:file].class == Array 

    @image = Image.new(p_attr) 
    if @image.save 
     respond_to do |format| 
     format.js 
     format.html { 
      render :json => [@image.to_jq_upload].to_json, 
      :content_type => 'text/html', 
      :layout => false 
     } 
     format.json { 
      render :json => { :files => [@image.to_jq_upload] }  
     } 
     end 
    else 
     render :json => [{:error => "custom_failure"}], :status => 304 
    end 

    end 


    private 

    def image_params 
     params.require(:image).permit(:gallery_id, file:[]) 
    end 

end 

<%= form_for(@gallery) do |f| %> 
    <div class="field"> 
    <%= f.label :title %><br> 
    <%= f.text_field :title %> 
    </div> 
    <div class="field"> 
    <%= f.label :desc %><br> 
    <%= f.text_field :desc %> 
    </div> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 
<%= form_for Image.new, :html => { :multipart => true, :id => "fileupload" } do |f| %> 
      <%= f.file_field :file, :id => "add_images_field", :multiple => true %> 
      <%= f.hidden_field :gallery_id, :value => @gallery.id %> 
<% end %> 

錯誤

Started POST "/images" for 127.0.0.1 at 2014-10-02 21:25:50 -0400 
Processing by ImagesController#create as JSON 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"RUCmH41pGzkzWurqqeQHJgsn3ejtrTbscdiGey9036E=", "image"=>{"gallery_id"=>"1", "file"=>[#<ActionDispatch::Http::UploadedFile:0x007ffbd6e2dbd0 @tempfile=#<Tempfile:/var/folders/3m/t1v11gzj32n0fdbhwr823y600000gn/T/RackMultipart20141002-83940-w9i67p>, @original_filename="10648423_1503708756533719_4977582933706996777_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image[file][]\"; filename=\"10648423_1503708756533719_4977582933706996777_o.jpg\"\r\nContent-Type: image/jpeg\r\n">]}} 
P_ATTR = {"gallery_id"=>"1", "file"=>#<ActionDispatch::Http::UploadedFile:0x007ffbd6e2dbd0 @tempfile=#<Tempfile:/var/folders/3m/t1v11gzj32n0fdbhwr823y600000gn/T/RackMultipart20141002-83940-w9i67p>, @original_filename="10648423_1503708756533719_4977582933706996777_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image[file][]\"; filename=\"10648423_1503708756533719_4977582933706996777_o.jpg\"\r\nContent-Type: image/jpeg\r\n">} 
Completed 500 Internal Server Error in 1ms 

ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError): 
    app/controllers/images_controller.rb:24:in `create' 

回答

0

所以...的解決方案如下......但我還是不明白,同樣的代碼在我的其他應用程序的工作原理,使用相同的軌道版本,我沒有使用這個許可證!線。

p_attr.permit!

class ImagesController < ApplicationController 
    def create 

    p_attr = params[:image] 
    p_attr[:gallery_id] = params[:image][:gallery_id] 
    p_attr[:file] = params[:image][:file].first if params[:image][:file].class == Array 

    p_attr.permit! 
    @image = Image.new(p_attr) 
    if @image.save 
     respond_to do |format| 
     format.js 
     format.html { 
      render :json => [@image.to_jq_upload].to_json, 
      :content_type => 'text/html', 
      :layout => false 
     } 
     format.json { 
      render :json => { :files => [@image.to_jq_upload] }  
     } 
     end 
    else 
     render :json => [{:error => "custom_failure"}], :status => 304 
    end 

    end