How to update feed using ajax in Rails 4.0

风格不统一 提交于 2019-12-11 03:18:37

问题


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.erb
class 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
        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? %>
    <div class="row">
        <aside class="col-md-4">
            <section class="user_info">
                <%= render 'shared/user_info' %>
            </section>
            <section class="stats">
                <%= render 'shared/stats' %>
            </section>
        </aside>
        <div class="col-md-8">
            <h3>Micropost Feed</h3>
            <section class="micropost_form">
                <%= render 'shared/feed_form' %>
            </section>
            <%= render 'shared/feed' %>
        </div>
    </div>
<% 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..." %>
    </div>
    <%= f.submit "Post", class: "btn btn-primary" %>
<% end %>


#_feed.html.erb

<% if @feed_items.any? %>
    <ol class="microposts">
    <%= render @feed_items %>
    </ol>
    <%= 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 .


回答1:


Here you simply need to render one thing that is feed for that in micheal heart book you simply add this line of code inside your create ajax file here is the code :

 $(".microposts").prepend('<%= j render  @feed %>');

Here you can get the result what you want .



来源:https://stackoverflow.com/questions/31473221/how-to-update-feed-using-ajax-in-rails-4-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!