James Osborn
James Osborn

Reputation: 187

Rails 3: Routing issue when trying to do an ajax checkbox

I know that there must be a really simple solution to the below problem but I have been trying to work it out for the last few hours.

I'm trying to update a table when a checkbox is checked (using ajax).

<% @tasks.each do |task| %>
<tr><td>

<%= form_for task, :url => {:controller =>"tasks", :action => "update" }, :remote =>true do |t| %>
<%= t.check_box :completed_at, :class => 'submittable'%> 
<% end %>

</td><td>
<h3><%=task.description %></h3></td> 
<td><%= link_to 'Edit Task',edit_list_task_path(@list, task), :id => "edit_#{task.id}",:class=>"btn info" %></td>
<td><%= link_to 'Delete Task', list_task_path(@list, task), :method => :delete, :confirm => "Are you sure?", :id => "delete_#{task.id}", :class=>"btn danger "%></td></tr></tr>

<%end%>

However I'm getting the following routing error:

No route matches {:controller=>"tasks", :action=>"update"} 

My current routing file looks like this:

  root :to => "home#index"
  resources :documents
  resources :timelines
  resources :lists
  resources :tasks
  resources :fundraisings
  resources :news
  get "home/index"
  devise_for :users, :lists, :fundraisings, :timelines
  resources :lists do
   resources :tasks
  end
  resources :users do
   resources :lists    
  end

Any idea why?

Upvotes: 0

Views: 264

Answers (2)

jake
jake

Reputation: 2411

Take a look at the way form_for understands RESTful resources here under the section Resource-oriented style.

I the above example try to just use a route helper ie :url => task_path(task) if you just trying to use the form for editing tasks and not creation. For a entire list of routes avaliable to you type rake routes on console.

Also if you use the correct url, you should be able to get form_for smartly POST (in case of create) or PUT (in case of update).

If you want to stick to above syntax, also try adding the :method => :put option to your form_for method.

Upvotes: 0

glortho
glortho

Reputation: 13198

With standard RESTful routing you don't need to specify the url. Does this not work?

form_for task, :remote =>true do |t|

Upvotes: 1

Related Questions