# Using the Sync gem to make restful Rails app real-time

**URL:** <https://www.ruby-forum.com/t/using-the-sync-gem-to-make-restful-rails-app-real-time/233477>\
**Category:** Rails\
**Created:** [December 16, 2014, 9:22pm UTC](https://www.ruby-forum.com/t/using-the-sync-gem-to-make-restful-rails-app-real-time/233477 "2014-12-16T21:22:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [December 16, 2014, 9:22pm UTC](https://www.ruby-forum.com/t/using-the-sync-gem-to-make-restful-rails-app-real-time/233477/1 "2014-12-16T21:22:31Z")

</div>

I’ve made a functional mini social network app in Rails. I’m using the  
Sync  
gem in order to make my app realtime so that when a user posts  
something,  
it’ll immediately show up on their home feed. I feel like I’m extremely  
close to making this work. The form works and there are no run-time  
errors  
with the below code. However, when the current user posts something, it  
doesn’t appear on the home page unless the page is refreshed. If it’s  
any  
help, I’m using Michael H.'s system to do this. What am I doing wrong  
here? Thanks for your help in advance. If I missed a piece of code that  
I  
should I have put here, the repository is on

> **[GitHub - jacobtarr/lemur: A social network that allows users to post events...](https://github.com/jacobtarr/lemur)**
>
> A social network that allows users to post events and news. - GitHub - jacobtarr/lemur: A social network that allows users to post events and news.

My code:

_app/controllers/microposts\_controller.rb_

…  
def create  
@micropost = current\_user.microposts.build(micropost\_params)  
if @micropost.save  
sync\_new @micropost  
flash[:success] = “Your post has been published!”  
else  
@feed\_items = []  
render ‘static\_pages/home’  
endend  
…

_app/views/static\_pages/home.html.erb_

\<% provide(:title, “Home”) %\>\<% if logged\_in? %\>  
\<% provide(:main\_class\_modifier, “–dashboard”) %\>

  
  
\<%= render ‘shared/dashboard/current\_user\_info’ %\>  
\<%= render ‘shared/dashboard/account\_settings’ %\>  
  
  
\<%= render ‘shared/dashboard/post\_widget’ %\>  
\<%= render ‘shared/dashboard/feed’ %\>  
  

\<% else %\>  
\<% provide(:main\_class\_modifier, “–homepage”) %\>  
\<%= render ‘static\_pages/home/home\_features’ %\>  
\<%= render ‘static\_pages/home/home\_newsletter’ %\>\<% end %\>

_app/views/shared/dashboard/\_feed.html.erb_

Your Feed

 \<% if @feed\_items.any? %\> 
 \<%= sync partial: 'micropost', collection: @feed\_items %\> \<%= sync\_new partial: 'micropost', resource: Micropost.new %\> 
 \<%= will\_paginate @feed\_items %\> \<% end %\> 

_app/views/sync/microposts/\_micropost.html.erb_

- 
 \<%= link\_to gravatar\_for(micropost.user, size: 100), micropost.user %\> \<%= link\_to at\_username micropost.user %\>

###### \<%= micropost.headline %\>

\<%= micropost.content %\>

- \<%= time\_ago\_in\_words(micropost.created\_at) %\> ago
- \<%= I18n.l micropost.date, :format =\> :default %\>
- \<%= micropost.location %\>
- \<% if current\_user?(micropost.user) %\> \<%= link\_to 'Delete'.html\_safe, micropost, remote: true, method: :delete, data: { confirm: "You sure?" } %\> \<% end %\> 

_app/views/shared/dashboard/\_post\_widget.html.erb_

Post

 \<%= form\_for @micropost, html: { class: "post-section\_\_form" }, remote: true do |f| %\> ... \<%= f.submit "Post", class: "btn btn\_\_primary btn\_\_lg btn\_\_post" %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![Jacob\_Tarr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@Jacob\_Tarr](https://www.ruby-forum.com/u/Jacob_Tarr)\
**Post date:** [December 17, 2014, 11:30am UTC](https://www.ruby-forum.com/t/using-the-sync-gem-to-make-restful-rails-app-real-time/233477/2 "2014-12-17T11:30:13Z")

</div>

You are not syncing, you are rendering.

In practice, one simply only needs to replace:

\<%= render partial: ‘user\_row’, locals: {user: @user} %\>

with:

\<%= sync partial: ‘user\_row’, resource: @user %\>

Look at the sync documentation here [GitHub - chrismccord/render\_sync: Real-time Rails Partials](https://github.com/chrismccord/sync)
