Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 5f3e9b6..8c515ad 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -13,4 +13,4 @@
//= require jquery
//= require jquery_ujs
//= require bootstrap
-//= require_tree .
\ No newline at end of file
+//= require_tree .
diff --git a/app/controllers/microposts_controller.rb b/app/controllers/microposts_controller.rb
index 435a78a..09ab6ad 100644
--- a/app/controllers/microposts_controller.rb
+++ b/app/controllers/microposts_controller.rb
@@ -6,7 +6,10 @@ class MicropostsController < ApplicationController
@micropost = current_user.microposts.build(params[:micropost])
if @micropost.save
flash[:success] = "Micropost created!"
- redirect_to root_path
+ respond_to do |format|
+ format.html { redirect_to root_path }
+ format.js
+ end
else
@feed_items = []
render 'static_pages/home'
@@ -24,4 +27,4 @@ class MicropostsController < ApplicationController
@micropost = current_user.microposts.find_by_id(params[:id])
redirect_to root_path if @micropost.nil?
end
-end
\ No newline at end of file
+end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index d034941..8035094 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -18,4 +18,4 @@
<%= debug(params) if Rails.env.development? %>
</div>
</body>
-</html>
\ No newline at end of file
+</html>
diff --git a/app/views/microposts/create.js.erb b/app/views/microposts/create.js.erb
new file mode 100644
index 0000000..30588e4
--- /dev/null
+++ b/app/views/microposts/create.js.erb
@@ -0,0 +1,2 @@
+$("#microposts").prepend("<%= escape_javascript( render partial: 'shared/feed_item', locals: { feed_item: @micropost } )%>");
+$("#micropost_content").val("");
diff --git a/app/views/shared/_feed.html.erb b/app/views/shared/_feed.html.erb
index 93387da..fa9c773 100644
--- a/app/views/shared/_feed.html.erb
+++ b/app/views/shared/_feed.html.erb
@@ -1,6 +1,6 @@
<% if @feed_items.any? %>
- <ol class="microposts">
+ <ol class="microposts" id="microposts">
<%= render partial: 'shared/feed_item', collection: @feed_items %>
</ol>
<%= will_paginate @feed_items %>
-<% end %>
\ No newline at end of file
+<% end %>
diff --git a/app/views/shared/_micropost_form.html.erb b/app/views/shared/_micropost_form.html.erb
index f08283b..ad0e947 100644
--- a/app/views/shared/_micropost_form.html.erb
+++ b/app/views/shared/_micropost_form.html.erb
@@ -1,7 +1,7 @@
-<%= form_for(@micropost) do |f| %>
+<%= form_for(@micropost, remote: true) do |f| %>
<%= render 'shared/error_messages', object: f.object %>
<div class="field">
<%= f.text_area :content, placeholder: "Compose new micropost..." %>
</div>
<%= f.submit "Post", class: "btn btn-large btn-primary" %>
-<% end %>
\ No newline at end of file
+<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment