Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 Hartl'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 https://github.com/jacobtarr/lemur

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'
   end
 end


app/views/static_pages/home.html.erb

XML
<% provide(:title, "Home") %>
    <% if logged_in? %>
        <% provide(:main_class_modifier, "--dashboard") %>
        <div class="dashboard">
            <aside class="dashboard__sidebar">
                <%= render 'shared/dashboard/current_user_info' %>
                <%= render 'shared/dashboard/account_settings' %>
            </aside>
            <section class="dashboard__content">
                <%= render 'shared/dashboard/post_widget' %>
                <%= render 'shared/dashboard/feed' %>
            </section>
        </div>
    <% 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
XML
<div class="dashboard__content--feed-section">
        <div class="pagelet">
            <div class="pagelet__header"><i class="fa fa-rss"></i>Your Feed</div>
            <div class="pagelet__body">
                <% if @feed_items.any? %>
                    <ul class="posts-list">
                        <%= sync partial: 'micropost', collection: @feed_items %>
                        <%= sync_new partial: 'micropost', resource: Micropost.new %>
                    </ul>
                  <%= will_paginate @feed_items %>
                <% end %>
            </div>
        </div>
    </div>


app/views/sync/microposts/_micropost.html.erb

XML
<li class="post" id="post-<%= micropost.id %>">
        <div class="post__avatar">
            <%= link_to gravatar_for(micropost.user, size: 100), micropost.user %>
            <span><%= link_to at_username micropost.user %></span>
        </div>
        <article class="post__content">
            <h6><%= micropost.headline %></h6>
            <p><%= micropost.content %></p>
        </article>
        <ul class="post__meta-info">
            <li><i class="fa fa-clock-o"></i><%= time_ago_in_words(micropost.created_at) %> ago</li>
            <li><i class="fa fa-calendar-o"></i><%= I18n.l micropost.date, :format => :default %></li>
            <li><i class="fa fa-map-marker"></i><%= micropost.location %></li>
            <li class="post__meta-info--delete">
            <% if current_user?(micropost.user) %>
                <%= link_to '<i class="fa fa-trash-o"></i>Delete'.html_safe, micropost,
                                                 remote: true,
                                                 method: :delete,
                                                 data: { confirm: "You sure?" } %>
            <% end %>
            </li>
        </ul>
    </li>


app/views/shared/dashboard/_post_widget.html.erb

XML
<div class="dashboard__content--post-section">
     <div class="pagelet">
         <div class="pagelet__header"><i class="fa fa-pencil"></i>Post</div>
         <div class="pagelet__body">
             <%= 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>
     </div>
 </div>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900