From 2448a43ec02a9a0a4e26cee42ff007abe8f6bc38 Mon Sep 17 00:00:00 2001 From: Daniel Yang Date: Sat, 26 Jan 2013 15:33:05 -0500 Subject: [PATCH 1/3] added edit article modal functionality my first fork --- app/controllers/articles_controller.rb | 2 +- app/views/articles/_article.html.erb | 2 +- app/views/articles/_update-form.html.erb | 21 +++++++++++++++++++++ app/views/articles/edit.html.erb | 10 ++++++---- public/javascripts/application.js | 17 +++++++++++++++++ 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 app/views/articles/_update-form.html.erb diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 60602ee..395687a 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -62,7 +62,7 @@ def update respond_to do |format| if @article.update_attributes(params[:article]) - format.html { redirect_to(@article, :notice => 'Article was successfully updated.') } + format.html { redirect_to(articles_path, :notice => 'Article was successfully updated.') } format.xml { head :ok } else format.html { render :action => "edit" } diff --git a/app/views/articles/_article.html.erb b/app/views/articles/_article.html.erb index edf405a..7440d9c 100644 --- a/app/views/articles/_article.html.erb +++ b/app/views/articles/_article.html.erb @@ -1,6 +1,6 @@ <%= article.title %> <%= link_to 'Show', article %> - <%= link_to 'Edit', edit_article_path(article) %> + <%= link_to 'Edit', edit_article_path(article), :class => 'edit' %> <%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %> diff --git a/app/views/articles/_update-form.html.erb b/app/views/articles/_update-form.html.erb new file mode 100644 index 0000000..7e6b1eb --- /dev/null +++ b/app/views/articles/_update-form.html.erb @@ -0,0 +1,21 @@ +<%= form_for @article do |f| %> + <% if @article.errors.any? %> +
+

<%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :title %>
+ <%= f.text_field :title %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/articles/edit.html.erb b/app/views/articles/edit.html.erb index 2c19833..926a706 100644 --- a/app/views/articles/edit.html.erb +++ b/app/views/articles/edit.html.erb @@ -1,6 +1,8 @@ -

Editing article

+
+

Editing article

-<%= render 'form' %> + <%= render 'update-form' %> -<%= link_to 'Show', @article %> | -<%= link_to 'Back', articles_path %> + <%= link_to 'Show', @article %> | + <%= link_to 'Back', articles_path %> +
\ No newline at end of file diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 20c83fb..4277983 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -2,6 +2,23 @@ // This file is automatically included by javascript_include_tag :defaults $(document).ready(function() { + $('.edit').click(function(e) { + var url = $(this).attr('href'); + var dialog_form = $('
Loading form...
').dialog({ + autoOpen: false, + width: 520, + modal: true, + open: function() { + return $(this).load(url + ' #content'); + }, + close: function() { + $('#dialog-form').remove(); + } + }); + dialog_form.dialog('open'); + e.preventDefault(); + }); + $('#create_article').click(function(e) { var url = $(this).attr('href'); var dialog_form = $('
Loading form...
').dialog({ From a1622000f93d2b9e07660df8509d2d89a6c63ea3 Mon Sep 17 00:00:00 2001 From: Daniel Yang Date: Sun, 27 Jan 2013 10:47:49 -0500 Subject: [PATCH 2/3] refactor modal code --- app/views/articles/_article.html.erb | 2 +- app/views/articles/index.html.erb | 2 +- public/javascripts/application.js | 19 +------------------ 3 files changed, 3 insertions(+), 20 deletions(-) diff --git a/app/views/articles/_article.html.erb b/app/views/articles/_article.html.erb index 7440d9c..56eaec5 100644 --- a/app/views/articles/_article.html.erb +++ b/app/views/articles/_article.html.erb @@ -1,6 +1,6 @@ <%= article.title %> <%= link_to 'Show', article %> - <%= link_to 'Edit', edit_article_path(article), :class => 'edit' %> + <%= link_to 'Edit', edit_article_path(article), :class => 'modal' %> <%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %> diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index 41d5d4e..85bf5aa 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -13,4 +13,4 @@
-<%= link_to 'New Article', new_article_path, :id => 'create_article' %> +<%= link_to 'New Article', new_article_path, :class => 'modal' %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 4277983..accf88c 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -2,24 +2,7 @@ // This file is automatically included by javascript_include_tag :defaults $(document).ready(function() { - $('.edit').click(function(e) { - var url = $(this).attr('href'); - var dialog_form = $('
Loading form...
').dialog({ - autoOpen: false, - width: 520, - modal: true, - open: function() { - return $(this).load(url + ' #content'); - }, - close: function() { - $('#dialog-form').remove(); - } - }); - dialog_form.dialog('open'); - e.preventDefault(); - }); - - $('#create_article').click(function(e) { + $('.modal').click(function(e) { var url = $(this).attr('href'); var dialog_form = $('
Loading form...
').dialog({ autoOpen: false, From 37ebc711a8651ca0523bcf6710b67ba165848d9e Mon Sep 17 00:00:00 2001 From: Daniel Yang Date: Mon, 28 Jan 2013 16:07:17 -0500 Subject: [PATCH 3/3] newly added article's edit also triggers ajax --- app/views/articles/create.js.erb | 1 + public/javascripts/application.js | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/views/articles/create.js.erb b/app/views/articles/create.js.erb index 86d9d97..b5a1d15 100644 --- a/app/views/articles/create.js.erb +++ b/app/views/articles/create.js.erb @@ -6,4 +6,5 @@ $('#dialog-form').dialog('close'); $('#dialog-form').remove(); $('table').append('<%= escape_javascript(render(@article)) %>'); + $('table tr:last .modal').click(attatchModal()); <%- end %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index accf88c..273a3f7 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -1,13 +1,15 @@ // Place your application-specific JavaScript functions and classes here // This file is automatically included by javascript_include_tag :defaults -$(document).ready(function() { +function attatchModal() { $('.modal').click(function(e) { var url = $(this).attr('href'); var dialog_form = $('
Loading form...
').dialog({ autoOpen: false, width: 520, modal: true, + show: 'fade', + hide: 'drop', open: function() { return $(this).load(url + ' #content'); }, @@ -18,4 +20,8 @@ $(document).ready(function() { dialog_form.dialog('open'); e.preventDefault(); }); -}); +} + +$(document).ready(function() { + attatchModal(); +}); \ No newline at end of file