【问题标题】:Params not being sent with jquery-file-upload未使用 jquery-file-upload 发送参数
【发布时间】:2014-01-20 18:06:19
【问题描述】:

由于某种原因,我的查询文件上传 ajax 表单出现以下错误:

ActionController::ParameterMissing (param not found: profile):
    app/controllers/profiles_controller.rb:146:in `profile_params'
    app/controllers/profiles_controller.rb:52:in `create'

这里是发送的参数:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"fdsahKJAHLSJFJAHFAS323FDSGF4="}

因此,由于某种原因,配置文件参数没有被发送。这是我的表格:

<%= form_for @profile, :html => { :class => "clearfix", :id => "the_form"},
    remote: true do |f| %>
    <%= f.file_field :pic, name: 'profile[pic]', :class => "field file-field",
    :id => "file", :multiple => true %>
<% end %>

这是我的强参数:

def profile_params
    params.require(:profile).permit(:title, :pic, :category, :description,
    :state, :zip_code, :rate)   
end 

这是加载表单的配置文件#new 操作:

def new
    @profile = Profile.new
@skills = Skill.all.collect {|skill| skill.label} #this is for something else on the page
end

另外,这里是获取表单的咖啡脚本文件。由于表单是通过ajax渲染的,所以我需要使用ajaxcomplete方法。

jQuery ->
  $(document).ajaxComplete ->
    $('#the_form').fileupload
      dataType: "script"
      add: (e, data) ->
        types = /(\.|\/)(gif|jpe?g|png)$/i
        file = data.files[0]
        if types.test(file.type) || types.test(file.name)
          data.submit()
        else
          alert("#{file.name} is not a gif, jpeg, or png image file")
      done: (e, data) ->
        $('#finish-text').html "Upload finished"

我知道咖啡脚本有效,因为它正在使用另一种形式。

为什么没有发送配置文件参数?

【问题讨论】:

    标签: ruby-on-rails ajax jquery ruby-on-rails-4 jquery-file-upload


    【解决方案1】:

    在表单中添加 :multipart => true,因为表单中有文件上传字段

    例如:

    { :class=> "clearfix", :id => "the_form", :multipart => true}, remote: true do |f| %>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 1970-01-01
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-05-28
      相关资源
      最近更新 更多