用于ajax调用的ActionController :: RoutingError(没有路由与[PUT]匹配)

时间:2018-12-13 17:06:50

标签: ruby-on-rails ruby ajax

希望一切都很好。

我想对_questions_form进行ajax调用。它没有remote: true选项就可以使用,但是我使用的是 actioncable ,并且当我提交表单时,我的 actioncable 数据在提交时就消失了,所以我认为使用ajax制作表单请致电以防止出现该问题。

我的_questions_form.html.erb

<%= form_for complete_tasks_path, :method => :put, :html=>{:remote=>true} do %>
  <ul>
  <% @in_completed_tasks.each do |task| %>
    <li>
      <%= check_box_tag "task_ids[]", task.id %>
      <!--<input type="checkbox" name="task_ids[]" id="task_ids_" value="5">-->
      <%= task.name %>
    </li>
  <% end %>
  </ul>
  <%= submit_tag "Submit your Answers" if @in_completed_tasks.present? %>
<% end %>

我的routes.rb

  resources :tasks do   
    collection do
      put :complete
    end      
  end

我的_result.js.erb

 $('#results').html("<%= j (render partial: 'tasks/questions_form') %>")

我的task_controller

class TasksController < ApplicationController
    def index
        @completed_tasks = Task.complete
        @in_completed_tasks = Task.in_complete
        @answers = Answer.all
    end

    def new
        @task = Task.new
    end

    def create
        @task = Task.create(allowed_tasks_params)
        if @task.save
            flash[:success] =  "Task was saved"
           redirect_to tasks_path
        else
            flash.now[:danger] = "something went wrong"
            render 'new'
        end
    end

    def complete

    #  byebug 
    #  Task.where(id: params[:task_ids]).update_all(complete: true)
    #   debugger 
    ids = params[:task_ids]
    if ids
        ids.each do |id|
            answer = Task.where(id: id).first.name
            Answer.create(feedback: answer)
            ActionCable.server.broadcast 'web_notifications_channel',
                                   message:  answer
        end
    end
    #  Task.update_all(["completed_at=?", Time.now], :id => params[:task_ids])
    #   redirect_to root_path

       respond_to do |format|  
            format.js  
        end  
    end


private 
   def allowed_tasks_params
       params.require(:task).permit(:name, :complete)
   end
end

我的index.html.erb

<h1>All Questions</h1>
<div id="result">
 <%= render partial: 'tasks/questions_form' %>
</div>

<ul>
<% @completed_tasks.each do |c| %>
    <li><%= c.name %></li>
<% end %>
</ul>
<%= link_to "New Question", new_task_path %> 
<hr/>

<h1>All the answers</h1>
  <div id="messages">  
  </div>
<hr /> 
  <%= pie_chart Answer.group(:feedback).count , suffix: "%", refresh: 60 %>  

但是在控制台中,我收到了这样的错误

ActionController::RoutingError (No route matches [PUT] "/tasks"):

actionpack (5.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:22:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
railties (5.2.2) lib/rails/engine.rb:524:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'

注意:如果需要更多信息,请让我发布它们

2 个答案:

答案 0 :(得分:1)

只需删除表单上的:method => :put,如果要创建,则需要将其发布,但是只需删除方法部分,即可使用。

那是因为用/tasks创建时,没有通过put方法到达resources的路径

答案 1 :(得分:1)

您的routes.rb应该生成一个路由

PUT /tasks/complete映射到TasksController#complete

但是您的表单正在执行

PUT /tasks(从您的日志中)代替,因此请求(即您的表单)有问题

您可以尝试以下方法吗?

resources :tasks do   
   collection do
     post :complete
  end      
end

<%= form_tag complete_tasks_path, format: :js, method: :post, remote: true do %>
  <ul>
    <% @in_completed_tasks.each do |task| %>
      <li>
        <%= check_box_tag "task_ids[]", task.id %>
        <!--<input type="checkbox" name="task_ids[]" id="task_ids_" value="5">-->
        <%= task.name %>
      </li>
    <% end %>
  </ul>
  <%= submit_tag "Submit your Answers" if @in_completed_tasks.present? %>
<% end %>