Reputation: 693
Rails 4.0.2, Ruby 2.0.0
View:
<% @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 %>
Route:
post 'tasks/update_status' => 'tasks#update_status', via: :patch
match 'tasks/update_status' => 'tasks#update_status', via: :patch
Model:
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();
Server:
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"]]
I have two questions:
[1] Why is this patching to "update" and not "update_status"?
[2] How can I pass the task id when the checkbox is ticked?
Upvotes: 0
Views: 162
Reputation: 54684
You have a route PATCH /tasks/:id
, which has the effect that update_status
is treated as the id of the task. Your routes should look like this:
resources :tasks do
patch :update_status, on: :member
end
and in your view
form_for(task, url: update_status_task_path(task), method: :patch, remote: true)
Upvotes: 1