我正在本地环境中使用 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/