控制器操作response_to格式错误ActionController :: UnknownFormat

时间:2018-07-24 15:08:41

标签: ajax format ruby-on-rails-5 actioncontroller respond-to

我在Ruby on Rails应用程序中控制器操作响应有问题。

所以,我的taskasset_controller中有两个操作很简单:

  def destroy

    if params[:id].present?
      @asset = TaskAsset.find(params[:id])
      authorize @asset, :destroy?

      @asset.destroy

      respond_to do |format|
        format.js { @asset.id }
      end
    end
  end



 def create
    @task = Task.find(params[:task_id])
    authorize @task

    respond_to do |format|
      if params[:images]
        params[:images].each { |image|
          @task.task_assets.create(image: image)
        }
      end
        @task_assets = @task.task_assets
        authorize @task_assets, :create?

      format.js { @task_assets }
    end
  end

我还定期创建了两种表单destroy.js.erb和create.js.erb,如果在销毁表单时销毁图像,我会得到已删除图像的响应ID,以便我用来隐藏信息的模态预览图片。

但是在执行创建操作时,我会遇到以下错误:

ActionController::UnknownFormat
Extracted source (around line #14):

    authorize @task

    respond_to do |format|
      if params[:images]
        params[:images].each { |image|
          @task.task_assets.create(image: image)

在创建动作响应格式的情况下,我缺少什么?

2 个答案:

答案 0 :(得分:1)

这可能与您在respond_to块内执行某些操作而没有先调用format.[your_format]有关。

尝试将代码更改为

respond_to do |format|      
  format.js do 
    if params[:images]
      params[:images].each { |image|
        @task.task_assets.create(image: image)
      }
    end
    @task_assets = @task.task_assets
    authorize @task_assets, :create?
    render @task_assets
  end
end

或其他选项

if params[:images]
  params[:images].each do |image|
    @task.task_assets.create(image: image)
  end
end
@task_assets = @task.task_assets
authorize @task_assets, :create?

respond_to do |format|      
  format.js { @task_assets }
end

关键是您在respond_to do |format| ... end内唯一可以做的就是呼叫一个或多个format.[some_format]{ what_to_do }

检查documentation以获得更多信息

答案 1 :(得分:0)

所以我的解决方案是:

$ pip3 --version
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    load_entry_point('pip==1.5', 'console_scripts', 'pip3')()
  File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 177, in main
    cmd_name, cmd_args = parseopts(initial_args)
  File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 129, in parseopts
    parser = create_main_parser()
  File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 122, in create_main_parser
    description = [''] + ['%-27s %s' % (i, j) for i, j in command_summaries]
  File "/usr/lib/python3.4/site-packages/pip/__init__.py", line 122, in <listcomp>
    description = [''] + ['%-27s %s' % (i, j) for i, j in command_summaries]
  File "/usr/lib/python3.4/site-packages/pip/commands/__init__.py", line 55, in get_summaries
    cmditems = _sort_commands(commands, commands_order)
  File "/usr/lib/python3.4/site-packages/pip/commands/__init__.py", line 88, in _sort_commands
    return sorted(list(cmddict.items()), key=keyfn)
TypeError: 'module' object is not callable


$ python3 --version
Python 3.4.6
  1. 将脚本 **<script src="http://malsup.github.com/jquery.form.js"></script>** **<%= form_for TaskAsset.new, url: task_assets_path(format: :js),remote: true, :html => {remote: true, :multipart => true,id: 'ajaxformtask' } do |form| %>** <%= hidden_field_tag :authenticity_token, form_authenticity_token %> <%= hidden_field_tag 'task_id', task.id %> <div class="form-group"> <div class="col-md-12"> <label>Select Images</label> </div> <%= file_field_tag "images[]", id: "imageUploadInput", type: :file, multiple: true, class:'form-control display-block' %> </div> <div class="actions"> <div class="padding-bottom-3x text-center"> <button type="button" id="cancel-add-task-assets" class="btn btn-lg btn-warning">Cancel</button> <%= form.submit "Save", id:"add-task-assets", class: 'btn btn-lg task-btn-color' %> </div> </div> <% end %> **<script> $(document).ready(function(){ $("#ajaxformtask").ajaxForm() }) </script>** 添加到 这里的项目只是我们将其置于局部视图的示例。
  2. 添加表单ID名称和(格式::js)

       {远程:             true,:multipart => true,id:'ajaxformtask'} ||| %>
  3. 添加:

    $(document).ready(function(){                    $(“#ajaxformtask”)。ajaxForm()})

这也是创建控制器动作的最终外观:

http://malsup.github.com/jquery.form.js