【问题标题】:Uploading with jquery-fileupload-rails on rails4 getting forbidden attributes error在 rails4 上使用 jquery-fileupload-rails 上传获取禁止属性错误
【发布时间】:2014-10-03 01:35:29
【问题描述】:

我有一个属于图库的图像模型。我的画廊编辑页面中有一个附加/单独的表单来上传图像并将它们与画廊相关联。我正在使用 Fog 和 Carrierwave 来上传到 Rackspace 文件。

我得到的错误是禁止属性,但它没有告诉我是哪个属性/参数导致了问题。我该如何进一步调试这个??

型号

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'

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 carrierwave fog jquery-fileupload-rails


    【解决方案1】:

    所以......解决方案如下......但我仍然不明白相同的代码在我的其他应用程序中是如何工作的,使用相同的 rails 版本,我不必使用这个许可证!行。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      相关资源
      最近更新 更多