From 495d492ba518bc5904ccb584dc3f48c2020769ff Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Wed, 1 Feb 2012 00:39:04 -0500 Subject: [PATCH] Got js.erb ajax update working --- app/controllers/habits_controller.rb | 20 +++++++++++- app/views/habits/_habit.html.erb | 8 +++++ app/views/habits/edit.html.erb | 2 +- app/views/habits/index.html.erb | 37 ++++++++++------------ app/views/habits/vote.js.erb | 1 + config/routes.rb | 1 + db/migrate/20120201040610_create_habits.rb | 7 ++-- 7 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 app/views/habits/_habit.html.erb create mode 100644 app/views/habits/vote.js.erb diff --git a/app/controllers/habits_controller.rb b/app/controllers/habits_controller.rb index 3a20ad6f83..217ab47c47 100644 --- a/app/controllers/habits_controller.rb +++ b/app/controllers/habits_controller.rb @@ -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 diff --git a/app/views/habits/_habit.html.erb b/app/views/habits/_habit.html.erb new file mode 100644 index 0000000000..8bacc18d02 --- /dev/null +++ b/app/views/habits/_habit.html.erb @@ -0,0 +1,8 @@ +
+ <%= 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 %> + + <%= link_to 'Edit', edit_habit_path(habit) %> +
diff --git a/app/views/habits/edit.html.erb b/app/views/habits/edit.html.erb index 7fa1cc6d7b..f4d13a5b70 100644 --- a/app/views/habits/edit.html.erb +++ b/app/views/habits/edit.html.erb @@ -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 %> diff --git a/app/views/habits/index.html.erb b/app/views/habits/index.html.erb index ecba2b0805..1995c60f14 100644 --- a/app/views/habits/index.html.erb +++ b/app/views/habits/index.html.erb @@ -1,28 +1,23 @@ -

Listing habits

+ - - - - - - - - - - +

Habits

+ - - - - - - - - +
  • <%= render :partial => "habit", :locals => { :habit => habit } %>
  • <% end %> -
    NameNotesDailyValue
    <%= habit.name %><%= habit.notes %><%= habit.daily %><%= habit.value %><%= link_to 'Show', habit %><%= link_to 'Edit', edit_habit_path(habit) %><%= link_to 'Destroy', habit, confirm: 'Are you sure?', method: :delete %>
    + + +

    ToDos

    +
    diff --git a/app/views/habits/vote.js.erb b/app/views/habits/vote.js.erb new file mode 100644 index 0000000000..38b61ac6ef --- /dev/null +++ b/app/views/habits/vote.js.erb @@ -0,0 +1 @@ +$('#<%= @habit.id %>').html("<%= escape_javascript(render :partial => "habit", :locals => { :habit => @habit }) %>") diff --git a/config/routes.rb b/config/routes.rb index a0d00972f9..90079f7d85 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ HabitTracker::Application.routes.draw do resources :habits + match 'habits/:id/vote' => 'habits#vote' devise_for :users diff --git a/db/migrate/20120201040610_create_habits.rb b/db/migrate/20120201040610_create_habits.rb index 6c251114a0..f275bf3fd5 100644 --- a/db/migrate/20120201040610_create_habits.rb +++ b/db/migrate/20120201040610_create_habits.rb @@ -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