Got js.erb ajax update working

This commit is contained in:
Tyler Renelle 2012-02-01 00:39:04 -05:00
parent 725e97ca4f
commit 495d492ba5
7 changed files with 51 additions and 25 deletions

View file

@ -5,7 +5,8 @@ class HabitsController < ApplicationController
# GET /habits
# GET /habits.json
def index
@habits = Habit.all
@habits = Habit.where(:daily=>false)
@todos = Habit.where(:daily=>true)
respond_to do |format|
format.html # index.html.erb
@ -83,4 +84,21 @@ class HabitsController < ApplicationController
format.json { head :no_content }
end
end
def vote
@habit = Habit.find(params[:id])
@habit.score += params[:vote].to_i
respond_to do |format|
if @habit.save
# format.html { redirect_to @habit, notice: 'Habit was successfully updated.' }
# format.json { head :no_content }
format.js
else
# format.html { render action: "edit" }
# format.json { render json: @habit.errors, status: :unprocessable_entity }
format.js
end
end
end
end

View file

@ -0,0 +1,8 @@
<div id="<%= habit.id %>">
<%= habit.name %>
(<%= habit.score %>)
<%= link_to "√", { :action => "vote", :id => habit.id, :vote => habit.value }, :remote=>true %>
<%= link_to "X", { :action => "vote", :id => habit.id, :vote => -habit.value }, :remote=>true %>
<!--<span class="popup"><%= habit.notes %><%= habit.value %></span>-->
<%= link_to 'Edit', edit_habit_path(habit) %>
</div>

View file

@ -2,5 +2,5 @@
<%= render 'form' %>
<%= link_to 'Show', @habit %> |
<%= link_to 'Destroy', @habit, confirm: 'Are you sure?', method: :delete %> |
<%= link_to 'Back', habits_path %>

View file

@ -1,28 +1,23 @@
<h1>Listing habits</h1>
<!-- <%= javascript_tag do %>
$("#habits").bind("ajax:complete", function(et, e){
$("#products-list").html(e.responseText); // insert content
});
<% end %> -->
<table>
<tr>
<th>Name</th>
<th>Notes</th>
<th>Daily</th>
<th>Value</th>
<th></th>
<th></th>
<th></th>
</tr>
<h2>Habits</h2>
<ul id="habits">
<% @habits.each do |habit| %>
<tr>
<td><%= habit.name %></td>
<td><%= habit.notes %></td>
<td><%= habit.daily %></td>
<td><%= habit.value %></td>
<td><%= link_to 'Show', habit %></td>
<td><%= link_to 'Edit', edit_habit_path(habit) %></td>
<td><%= link_to 'Destroy', habit, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<li><%= render :partial => "habit", :locals => { :habit => habit } %></li>
<% end %>
</table>
</ul>
<h2>ToDos</h2>
<ul id="todos">
<% @todos.each do |habit| %>
<li><%= render :partial => "habit", :locals => { :habit => habit } %></li>
<% end %>
</ul>
<br />

View file

@ -0,0 +1 @@
$('#<%= @habit.id %>').html("<%= escape_javascript(render :partial => "habit", :locals => { :habit => @habit }) %>")

View file

@ -1,5 +1,6 @@
HabitTracker::Application.routes.draw do
resources :habits
match 'habits/:id/vote' => 'habits#vote'
devise_for :users

View file

@ -3,8 +3,11 @@ class CreateHabits < ActiveRecord::Migration
create_table :habits do |t|
t.string :name
t.text :notes
t.boolean :daily
t.integer :value
t.boolean :daily, :default => true
t.boolean :up, :default => true
t.boolean :down, :default => true
t.integer :value, :default => 1
t.integer :score, :default => 0
t.timestamps
end