Topic: toggle checkbox, change class name thru RJS and save to database

Hi! I'm toggling a list of items using a checkbox and have managed to toggle the class name using this in my partial:

<div id="tasks">

<% @timeframes.each do |timeframe| %>

<div id="<%= timeframe.shortcut %>">
    <h2><%= timeframe.name %></h2>
    <ul>
        <% timeframe.tasks.each do |task| %>
        <li id="task_<%= task.id %>" class="<%= 'completed' if task.completed == 1 %>">
        <div class="checkbox"><%= check_box :task, :completed, :onchange => "$('task_#{task.id}').toggleClassName('completed');" %></div>
          <div class="description"><%= task.description %></div>
    <% end %>
    </ul>
</div>

<% end %>

</div>


However, I would like this to be solved using RJS; page.toggle_class_name perhaps?. Also I can't seem to get tasks that are checked to show as checked when loading the page and it would also be great if the database-record could be updated :onchange.

How would I go about this in the best way?

Re: toggle checkbox, change class name thru RJS and save to database

Totally untested:

<%= check_box :task, :completed, {:onChange => remote_function(:update => 'completed', :url => {:action => :your_action_here}, :with => "'task[completed]=' + value")}, task.include?(completed) %>

Last edited by RailsRocks (2007-10-09 11:20:32)

http://www.webyfi.com
It's not the size of the app that matters, it's how you code it. ~ railsenvy

Re: toggle checkbox, change class name thru RJS and save to database

I solved part of it by:

view-partial: _tasks.rjs

<%= check_box :task, :completed, :onchange => remote_function(:url => {:action => :toggle_completed, :id => task}) %>

controller: task_controller.rb
  def index
    @task = Task.new
   
    @projects = Project.find(:all)
    @tasks = Task.find(:all)
    @timeframes = Timeframe.find(:all)
  end

  def toggle_completed
    @task = Task.find(params[:id])
   
    @task.notes = "hej"
   
    if @task.completed == 1
      @task.completed = 0
    else
      @task.completed = 1
    end
   
    @task.save
   
  end


RJS-template: toggle_completed.rjs
page[:task_#{task.id}].toggle_class_name "completed"

However I can't seem to get the task_id to display correctly in the RJS-template. I've tried to just change #{task.id} to for ex. ":task_144" to make sure the toggle_class works but I can't seem to pass the id from the view to the RJS-template for each item. Any ideas?

Last edited by Gustav (2007-10-10 03:21:02)

Re: toggle checkbox, change class name thru RJS and save to database

Got! Not sure if it's the best way, but it works wink

_task.rhtml
<%= check_box :task, :completed, :onchange => remote_function( :url => { :action => 'toggle_completed', :id => task }, :with => "'id=#{task.id}'" ) %>

toggle_completed.rjs
page["task_#{params[:id]}"].toggle_class_name "completed"


Now, if I could just get those checkboxes to show up us checked when I load the page the first time...

Re: toggle checkbox, change class name thru RJS and save to database

hi gustav iam trying something similar to you like save or move the cheked value into session as soon as they checked,i dont have any idea about rjs as am new to ruby...please let me know where to add rjs file and what class and action should be in there  and what should be in controller attached to the view etc..if you can please post the exmaple

thank you