ActionController :: RoutingError(没有路由匹配[DELETE]“/tasks.1”)

时间:2015-04-18 02:18:41

标签: 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>

应用/视图/页/ 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();

配置/ 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 call'   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 call'   actionpack(4.2.1)lib / action_dispatch / middleware / show_exceptions.rb:30:in`call'

...

佣金路线

    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

4 个答案:

答案 0 :(得分:2)

这必须解决。

在您的链接中尝试传递,

用于编辑:

edit_task_path(id: task.id)

用于删除或显示页面

task_path(id: task.id)

答案 1 :(得分:0)

删除/编辑的链接错误。正确的是/tasks/1/tasks/edit/1

在你的erb中,你提供了link_to的错误链接。

<td><%= link_to "Delete", tasks_path(task), method: :delete, remote: true, data: { confirm: "Are you sure you want to delete task #{task.title}?" }, class: "btn btn-primary" %>
    <%= link_to "Edit", edit_tasks_path(task), remote: true, class: "btn btn-primary" %>
</td>

应该是task_path(task)edit_task_path(task),而不是task[s]_path

答案 2 :(得分:0)

链接应该是:

<%= link_to "Delete", task_path(task), 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(task), remote: true, class: "btn btn-primary" %>

答案 3 :(得分:0)

从这个帖子的年代来看,你已经想出了如何解决这个问题或者已经转向了其他方面。

我注意到这一行

resource :tasks, only: [:new, :create, :edit, :destory]

你拼错了破坏这个词。查看您的rake路由导出我没有看到列出的DELETE操作。尝试将路由文件更新到此

resource :tasks, only: [:new, :create, :edit, :destroy]