0

I've got a dropdown list for users to select whether a task is completed or not. I want their changes to update the status on the database, without them having to click a submit button every time they make a change. Is this possible?

Basically, all tasks are marked as not complete by default, so when the user clicks the dropdown and changes it to complete, I want this to change on the database without them having to click a submit button.

Any help anyone could give would be great, thanks!

This is the code I have:

<%= form_for task do %>
    <td><%= select :task, :complete, [ ["Yes",2], ["Partly",1], ["No",0]], { :action => "update" } %></td>
<% end %>
ecs
  • 708
  • 1
  • 14
  • 33

3 Answers3

2

try adding this:

:onchange => "this.form.submit();"

take a look at RJS: Ajaxified select_tag for more detail how to use

Community
  • 1
  • 1
Tobas
  • 351
  • 1
  • 6
  • Thanks - that has nearly worked - it has updated the database with the correct info, but the page reloads and still has 'Yes' in the box - how can I get it to display the actual contents of the field in the database? – ecs Jul 03 '12 at 10:13
  • `:selected => ...` I think you need to change the first line to have access to your stored data. Something like `<%= form_for @task do |t| %>`. Then you could use `<% t.select ..., :selected => t.complete`. Maybe it depends on your rails version – Tobas Jul 03 '12 at 10:31
1

You can easily do this with ajax. You can listen to the onchange event on the select tag.

This is the best ajax on rails guide on the net imo, so maybe you can get some tips from it.

stephenmurdoch
  • 34,024
  • 29
  • 114
  • 189
0

There are several gems that utilize JSON requests to poll the models. I recommend watching this video. Ryan goes into detail on how to use the gem to do in place editing on a page.

Dominic Bou-Samra
  • 14,799
  • 26
  • 100
  • 156