【问题标题】:ActionController::RoutingError (No route matches [PUT] ) for ajax callajax 调用的 ActionController::RoutingError (No route matches [PUT] )
【发布时间】:2018-12-13 17:06:50
【问题描述】:

希望一切都好。

我想对_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'

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

【问题讨论】:

    标签: ruby-on-rails ruby ajax


    【解决方案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 %>
    

    【讨论】:

      【解决方案2】:

      只需删除表单上的:method =&gt; :put,如果要创建,则需要发布,但只需删除方法部分即可。

      那是因为当您使用 resources 创建 /tasks 时,没有使用 put 方法的路由

      【讨论】:

      • 现在我收到此错误 ActionController::ParameterMissing (param is missing or value is empty: task): app/controllers/tasks_controller.rb:48:in allowed_tasks_params' app/controllers/tasks_controller.rb:13:in create'
      猜你喜欢
      • 1970-01-01
      • 2022-12-26
      • 2016-03-15
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多