如何在Rails 4.0中使用Ajax更新提要 [英] How to update feed using ajax in Rails 4.0

查看:57
本文介绍了如何在Rails 4.0中使用Ajax更新提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为michael heart book的示例应用程序创建ajax功能.我创建了一个提要模型控制器,该控制器在Listen book中是micropost.我正在尝试使用ajax之类的post feed方法.但是我遇到了一些错误.我在下面附加我的代码,以帮助我.

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

结束

我的主页正在渲染部分内容,这是代码:

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

这是两个代码,一切正常,但是当我尝试添加ajax时出现错误:

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'

要在create方法上执行ajax,我在feeds提要视图中添加了这些文件,文件名为create.js.erb

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") %>");

表单文件位于名为_feed_form.html.erb的共享视图文件下

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 .

推荐答案

在这里,您只需要渲染一件事即可,这在micheal heart book中,您只需在创建ajax文件中添加以下代码即可,这是代码:

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 .

这篇关于如何在Rails 4.0中使用Ajax更新提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆