Rails 4.0.2,Ruby 2.0.0
查看:
<% @tasks_today.each do |task| %>
<%= form_for(task, :remote => true, :url => { :method => "patch", :action =>"update_status" }) do |f| %>
<%= check_box_tag :task, "yes", "no", {:class => 'checkable'} %>
<%= task.title =%>
<%= link_to 'Not Today', remove_from_today_path(task), method: :delete %>
<% end %>
<% end %>
路线:
post 'tasks/update_status' => 'tasks#update_status', via: :patch
match 'tasks/update_status' => 'tasks#update_status', via: :patch
型号:
def update_status
@task = Task.find(params[:id])
@status = Status.find_by name: params[:status]
@task.update_status(@status)
load
render 'index'
end
def update
@task = Task.find(params[:id])
if @task.update(params[:task].permit(:title, :text))
redirect_to tasks_path
else
render 'edit'
end
end
jQuery的:
jQuery ->
$('.checkable').on 'change', ->
$(this).parents('form:first').submit();
服务器:
Started PATCH "/tasks/update_status?method=patch" for 127.0.0.1 at 2014-06-02 19:29:50 -0400
Processing by TasksController#update as JS
Parameters: {"utf8"=>"✓", "method"=>"patch", "id"=>"update_status"}
Task Load (0.4ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT 1 [["id", "update_status"]]
我有两个问题:
[1] 为什么要修补“更新”而不是“update_status”?
[2] 勾选复选框后如何传递任务ID?
答案 0 :(得分:1)
您有一条路线PATCH /tasks/:id
,其效果是update_status
被视为任务的ID。您的路线应如下所示:
resources :tasks do
patch :update_status, on: :member
end
并在您的视图中
form_for(task, url: update_status_task_path(task), method: :patch, remote: true)