Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

applying ajax to project #4

Open
wants to merge 4 commits into
base: day4start
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/projects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$(document).on('ajax:success', function(e, data) {
console.log('Ajax Response data:', data);
});
3 changes: 3 additions & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ProjectsController < ApplicationController
# GET /projects.json
def index
@projects = Project.all
@project = Project.new
end

# GET /projects/1
Expand Down Expand Up @@ -48,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 }
Expand Down Expand Up @@ -76,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

Expand Down
2 changes: 1 addition & 1 deletion app/views/projects/_form.html.haml
Original file line number Diff line number Diff line change
@@ -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:"
Expand Down
9 changes: 9 additions & 0 deletions app/views/projects/_show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%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?' }, remote: true

6 changes: 6 additions & 0 deletions app/views/projects/create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// console.log("hello world from 'create.js.erb'")

(function () {
var projectHtml = "<%= j render('show', :project => @project) %>";
$('.projects').prepend(projectHtml);
})();
3 changes: 3 additions & 0 deletions app/views/projects/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(function () {
$('tr[data-project-id="<%= @project.id %>"').remove();
})();
13 changes: 4 additions & 9 deletions app/views/projects/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Active Projects
%small= link_to 'New Project', new_project_path

=render 'form'

.row
.large-9.large-centered.columns
%table
Expand All @@ -12,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?' }
=render 'show', project: project