Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

ruby on rails - ActionController::RoutingError (No route matches [PUT] ) for ajax call

Hope all is well.

I want to ajax call on _questions_form. It works without remote: true option, yet I am using actioncable and when I submit the form my actioncable data disappeared upon submitting , so I thought making the form with ajax call to prevent that issue.

my _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 %>

my routes.rb

  resources :tasks do   
    collection do
      put :complete
    end      
  end

my _result.js.erb

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

my 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

my 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 %>  

but in console I received such an error

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'

Note: Please if more info is needed, ask me to post them

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

just remove the :method => :put that you have on your form, if you will create, it needs to be post, but just remove the method part, and it will work.

thats because there is no route to /tasks with put method when you create it with resources


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...