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 .

feeds_controller.erbclass FeedsController < ApplicationController

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

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

  def destroy
  end

  private

    def feed_params
      params.require(:feed).permit(:content)
    end
end

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

static_pages/home.html.erb

<% if user_signed_in? %>



<%= f.submit “Post”, class: “btn btn-primary” %>
<% end %>

_feed.html.erb

<% 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

On Jul 17, 2015, at 4:35 AM, nilay singh [email protected] wrote:

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

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.)


Scott R.
[email protected]
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs