javascript - ActionController::RoutingError(没有路由匹配 [DELETE] "/tasks.1")

标签 javascript ruby-on-rails ruby-on-rails-4 rails-routing

我正在本地环境中使用 Rails 4.2.1 学习有关 TODO 应用程序的教程。

问题:/task.1,但它应该是/task/1。由 Nithin 解决。 我还在 config/routes.rb 中将“resource :tasks”编辑为“:task”。

这些解决了“编辑”问题!!!

但在“删除”时仍然出现某种路由错误。为什么?

Tasks_controller

class TasksController < ApplicationController
  respond_to :html, :js

  # GET /tasks
  # GET /tasks.json
  def new
    @task = Task.new    
    render :show_form
  end

  # POST /task
  # POST /task.json
  def create
    @task = Task.create(task_params)
    @tasks = Task.all
  end

  # GET /task
  # GET /task.json
  def edit
    @task = Task.find(params[:id])
    render :show_form
  end

  # DELETE /task
  # DELETE /task.json
  def destory
    @task = Task.find(params[:id])
    @task.destroy
    @tasks = Task.all
  end

  private

  def task_params
    params.require(:task).permit(:title, :note, :completed)
  end
end

_task_list.html.erb

<% if @tasks.empty? %>
  <span class="text-warning">There are no tasks!</span>
<% else %>
  <table class="table table-hover table-bordered">
    <thead>
        <tr>
            <th>Title</th>
            <th>Created at</th>
            <th>Completed</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <% @tasks.each do |task| %>
            <tr>
                <td><strong><%= task.title %></strong></td>
                <td class="text-info"><%= task.created_at %></td>
                <td class="text-success"><%= task.completed %></td>
                <td><%= link_to "Delete", task_path(id: task.id), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete task #{task.title}?" }, class: "btn btn-primary" %>
                    <%= link_to "Edit", edit_task_path(id task.id), remote: true, class: "btn btn-primary" %>
                </td>
            </tr>
        <% end %>
    </tbody>
</table>

app/views/pages/home.html.erb

<div id="task-list" class="container">
  <%= render 'tasks/task_list', locals: {task: @task} %>
</div>
<div id="modal" class="modal fade"></div>

destroy.js.erb

$('#task-list').html('<%= j(render 'task_list', locals: {task: @task} )%>');

edit.js.erb

m = $('#modal');
m.html('<%= j(render 'task_form', locals: {task: @task}) %>');
m.modal('show');
$('#modal').integrateDatepicker();

config/routes.rb

Rails.application.routes.draw do
  # Tasks
  resource :tasks, only: [:new, :create, :edit, :destory]

  root to: 'pages#home'
end

日志:删除

Started DELETE "/task?id=1" for 127.0.0.1 at 2015-04-18 12:00:33 +0700

ActionController::RoutingError(没有路由匹配 [DELETE]“/task”): actionpack (4.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in call' newrelic_rpm (3.11.2.286) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in调用' web-console (2.1.2) lib/web_console/middleware.rb:37:in call' newrelic_rpm (3.11.2.286) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in调用' actionpack (4.2.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

...

rake 动路线

    Prefix    Verb URI Pattern           Controller#Action
    tasks     POST /tasks(.:format)      tasks#create
    new_task  GET  /tasks/new(.:format)  tasks#new
    edit_task GET  /tasks/edit(.:format) tasks#edit
    root      GET  /                     pages#home

最佳答案

这个问题必须解决。

在您的链接中尝试传递类似,

编辑:

edit_task_path(id: task.id)

用于删除或显示页面

task_path(id: task.id)

关于javascript - ActionController::RoutingError(没有路由匹配 [DELETE] "/tasks.1"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29712189/

相关文章:

mysql - rake 数据库 :test:prepare does not create `table view`

ruby-on-rails-4 - bootstrap3 datetimepicker 无法加载语言环境

ruby-on-rails - 按 time desc 和 total_votes 呈现帖子

javascript - 数组中数组的 Angular 格式?

javascript - 让光标显示在输入焦点上

ruby-on-rails - 将符号数组传递给 Rails 的 `except()!` 方法

ruby-on-rails - Rails 表单文本字段助手,如何使输入区域更宽?

javascript - 有没有办法从对象中获取元素的数量?

javascript - XMLHttpRequest 开放访问被拒绝

ruby-on-rails - 一种方法有两个 before_action。哪个 before_action 将首先被调用?