gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:50:09 24 4
gpt4 key购买 nike

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

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

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

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

任务 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
调用' 网络控制台 (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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com