From 9721030f9b149faf60fa65c2a1d3b4d1fddc5e12 Mon Sep 17 00:00:00 2001 From: leejaew Date: Thu, 17 Oct 2013 09:45:38 -0500 Subject: [PATCH 1/4] make form work with ajax --- app/assets/javascripts/projects.js | 4 ++++ app/controllers/projects_controller.rb | 1 + app/views/projects/_form.html.haml | 2 +- app/views/projects/index.html.haml | 2 ++ 4 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/projects.js b/app/assets/javascripts/projects.js index e69de29..52506b3 100644 --- a/app/assets/javascripts/projects.js +++ b/app/assets/javascripts/projects.js @@ -0,0 +1,4 @@ +$(document).on('ajax:success', function(e, data) { + // console.log('Ajax Response data:', data); + console.log('Ajax Event:', e); +}); \ No newline at end of file diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 165cb9a..3750019 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -5,6 +5,7 @@ class ProjectsController < ApplicationController # GET /projects.json def index @projects = Project.all + @project = Project.new end # GET /projects/1 diff --git a/app/views/projects/_form.html.haml b/app/views/projects/_form.html.haml index c8b92c6..7fded40 100644 --- a/app/views/projects/_form.html.haml +++ b/app/views/projects/_form.html.haml @@ -1,4 +1,4 @@ -= form_for @project do |f| += form_for @project, remote: true do |f| - if @project.errors.any? #error_explanation %h2= "#{pluralize(@project.errors.count, "error")} prohibited this project from being saved:" diff --git a/app/views/projects/index.html.haml b/app/views/projects/index.html.haml index 2cf26e1..755c96d 100644 --- a/app/views/projects/index.html.haml +++ b/app/views/projects/index.html.haml @@ -2,6 +2,8 @@ Active Projects %small= link_to 'New Project', new_project_path + =render 'form' + .row .large-9.large-centered.columns %table From 77d18d2eb5a4301b98a8a1f8087a1821b1ab2ab7 Mon Sep 17 00:00:00 2001 From: leejaew Date: Thu, 17 Oct 2013 10:07:58 -0500 Subject: [PATCH 2/4] add js template to create method on projects controller --- app/assets/javascripts/projects.js | 3 +-- app/controllers/projects_controller.rb | 1 + app/views/projects/create.js.erb | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 app/views/projects/create.js.erb diff --git a/app/assets/javascripts/projects.js b/app/assets/javascripts/projects.js index 52506b3..13d8147 100644 --- a/app/assets/javascripts/projects.js +++ b/app/assets/javascripts/projects.js @@ -1,4 +1,3 @@ $(document).on('ajax:success', function(e, data) { - // console.log('Ajax Response data:', data); - console.log('Ajax Event:', e); + console.log('Ajax Response data:', data); }); \ No newline at end of file diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 3750019..d667ba9 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -49,6 +49,7 @@ def create if @project.save format.html { redirect_to projects_url, notice: 'Project was successfully created.' } format.json { render action: 'show', status: :created, location: @project } + format.js { render layout: false } else format.html { render action: 'new' } format.json { render json: @project.errors, status: :unprocessable_entity } diff --git a/app/views/projects/create.js.erb b/app/views/projects/create.js.erb new file mode 100644 index 0000000..f2b761c --- /dev/null +++ b/app/views/projects/create.js.erb @@ -0,0 +1 @@ +alert('Project created!'); From 947dceff49ed817c7e64069fa474a69f3632d896 Mon Sep 17 00:00:00 2001 From: leejaew Date: Thu, 17 Oct 2013 10:23:54 -0500 Subject: [PATCH 3/4] create new partial view for proejct HTML and render it without the application layout --- app/views/projects/_show.html.haml | 8 ++++++++ app/views/projects/create.js.erb | 7 ++++++- app/views/projects/index.html.haml | 11 ++--------- 3 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 app/views/projects/_show.html.haml diff --git a/app/views/projects/_show.html.haml b/app/views/projects/_show.html.haml new file mode 100644 index 0000000..42975eb --- /dev/null +++ b/app/views/projects/_show.html.haml @@ -0,0 +1,8 @@ +%tr + %td + = link_to project.name, project_url(project.id) + = "(#{project.phase.name})" + %td.text-center= "✓" unless current_user.subscriptions.where(project_id: project.id).blank? + %td= project.updated_at.strftime("%m/%d/%y at %I:%M%P") + %td= link_to 'Edit', edit_project_path(project.id) + %td= link_to 'Destroy', project_path(project.id), method: :delete, data: { confirm: 'Are you sure?' } \ No newline at end of file diff --git a/app/views/projects/create.js.erb b/app/views/projects/create.js.erb index f2b761c..4a0dbff 100644 --- a/app/views/projects/create.js.erb +++ b/app/views/projects/create.js.erb @@ -1 +1,6 @@ -alert('Project created!'); +// console.log("hello world from 'create.js.erb'") + +(function () { + var projectHtml = "<%= j render('show', :project => @project) %>"; + $('.projects').prepend(projectHtml); +})(); \ No newline at end of file diff --git a/app/views/projects/index.html.haml b/app/views/projects/index.html.haml index 755c96d..dfb93c4 100644 --- a/app/views/projects/index.html.haml +++ b/app/views/projects/index.html.haml @@ -14,13 +14,6 @@ %th Last updated %th %th - %tbody + %tbody.projects - @projects.each do |project| - %tr - %td - = link_to project.name, project_url(project.id) - = "(#{project.phase.name})" - %td.text-center= "✓" unless current_user.subscriptions.where(project_id: project.id).blank? - %td= project.updated_at.strftime("%m/%d/%y at %I:%M%P") - %td= link_to 'Edit', edit_project_path(project.id) - %td= link_to 'Destroy', project_path(project.id), method: :delete, data: { confirm: 'Are you sure?' } \ No newline at end of file + =render 'show', project: project From c17c0e80732433f85c3215728bf6fa5c2a1fbf48 Mon Sep 17 00:00:00 2001 From: leejaew Date: Thu, 17 Oct 2013 10:42:28 -0500 Subject: [PATCH 4/4] apply ajax to destory method, add destory.js.erb that selects the designated data row --- app/controllers/projects_controller.rb | 1 + app/views/projects/_show.html.haml | 5 +++-- app/views/projects/destroy.js.erb | 3 +++ 3 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 app/views/projects/destroy.js.erb diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index d667ba9..17d3ad1 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -78,6 +78,7 @@ def destroy respond_to do |format| format.html { redirect_to projects_url } format.json { head :no_content } + format.js { render layout: false } end end diff --git a/app/views/projects/_show.html.haml b/app/views/projects/_show.html.haml index 42975eb..121f5d7 100644 --- a/app/views/projects/_show.html.haml +++ b/app/views/projects/_show.html.haml @@ -1,8 +1,9 @@ -%tr +%tr{"data-project-id" => "#{project.id}"} %td = link_to project.name, project_url(project.id) = "(#{project.phase.name})" %td.text-center= "✓" unless current_user.subscriptions.where(project_id: project.id).blank? %td= project.updated_at.strftime("%m/%d/%y at %I:%M%P") %td= link_to 'Edit', edit_project_path(project.id) - %td= link_to 'Destroy', project_path(project.id), method: :delete, data: { confirm: 'Are you sure?' } \ No newline at end of file + %td= link_to 'Destroy', project_path(project.id), method: :delete, data: { confirm: 'Are you sure?' }, remote: true + \ No newline at end of file diff --git a/app/views/projects/destroy.js.erb b/app/views/projects/destroy.js.erb new file mode 100644 index 0000000..4abe074 --- /dev/null +++ b/app/views/projects/destroy.js.erb @@ -0,0 +1,3 @@ +(function () { + $('tr[data-project-id="<%= @project.id %>"').remove(); +})(); \ No newline at end of file