How to update feed using ajax in Rails 4.0

I am trying to create an ajax functionality for a sample app of michael heart book . I created a feed model controller which is micropost in hear book . I am trying to use ajax like post feed method . But I am getting some error . I am attaching my code below take a look and help me .

class FeedsController < ApplicationController

        before_action :authenticate_user! ,only: [:create, :destroy]
    def create
@feed =
        if @feed.           save
flash[:success] = "Micropost created!"
          #redirect_to root_url
          respond_to do |format|

          format.html { redirect_to root_url }
          format.        js
          @feed_items = []
          render 'static_pages/home'

      def destroy

def feed_params

And my home page is rendering some partials Here is the code :


<% if user_signed_in? %>
        <div class="row">
          <aside class="col-md-4">
            <section class="user_info">
              <%= render 'shared/user_info' %>
             <section class="stats">
              <%= render 'shared/stats' %>

          <div class="col-md-8">
            <h3>Micropost Feed</h3>
            <section class="micropost_form">
              <%= render 'shared/feed_form' %>
            <%= render 'shared/feed' %>
      <% else %>

      <% end %>
Here are two codes Everything work perfectly but when I am trying to add ajax I get the error which is :

  Rendered shared/_feed.html.erb (3.1ms)
          Rendered feeds/create.js.erb (6.0ms)
        Completed 500 Internal Server Error in 73ms (ActiveRecord: 44.4ms)

        NoMethodError (undefined method               `any?' for nil:NilClass):

app/views/shared/_feed.html.erb:1:in _app_views_shared__feed_html_erb___516512175__638262078 ' app/views/feeds/create.js.erb:1:in _app_views_feeds_create_js_erb___967625174__637838198’ app/controllers/feeds_controller.rb:9:in `create’

To execute ajax on create method I added these files unders feeds views file name is create.js.erb

$(“#feed_add”).html(“<%= escape_javascript(render partial: “shared/feed”) %>”);

And form file is under shared view file with name _feed_form.html.erb

<%= form_for(@feed,:remote => true) do |f| %>

      <div id="feed_add">
        <%= f.text_area :content, placeholder: "Compose new feed..." %>
      <%= f.submit "Post", class: "btn btn-primary" %>
    <% end %>


<% if @feed_items.any? %>

    <%= render @feed_items %>
<%= will_paginate @feed_items %> <% end %> ``` ```

So tell me how can I add feed and without refreshing how can I show feed using partial In this code

This means that you've got nil where you think you have an object. So, looking at the file & line reported, you see that in _feed.html.erb @feed_items must be nil.

So, two questions. Are you sure your AJAX post creates @feed_items? And do you understand what data is available to partials? (Hint, that second question is important, because partials do not get rendered in the same environment with the same object available.)