Javascript 通过jquery使用集合更新rails部分

Javascript 通过jquery使用集合更新rails部分,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,我有一个允许用户发布更新的表单。一旦用户发布更新,我希望更新列表刷新。为了实现这一点,我使用了Ajax和jQuery以及Rails。但是,我在试图让jquery呈现部分后期提要时遇到了麻烦 这是我正在使用的jquery $(".microposts").html("<%= j render partial: 'shared/feed_item', collection: @feed_items %>") $(“.microscops”).html(“”) 目前,提要只是刷新,没有

我有一个允许用户发布更新的表单。一旦用户发布更新,我希望更新列表刷新。为了实现这一点,我使用了Ajax和jQuery以及Rails。但是,我在试图让jquery呈现部分后期提要时遇到了麻烦

这是我正在使用的jquery

$(".microposts").html("<%= j render partial: 'shared/feed_item', collection: @feed_items %>")
$(“.microscops”).html(“”)
目前,提要只是刷新,没有显示任何内容。我相信这是因为我试图传递@feed_项的方式。传递该变量的最佳方式是什么

有人要控制器

class MicropostsController < ApplicationController
before_action :signed_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy

def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
        respond_to do |format|
            format.html { redirect_to root_url }
            format.js 
        end
    else
        @feed_items = []
        flash[:error] = "Failed to create micropost!"
        render 'static_pages/home'
    end
end

def destroy 
    @micropost.destroy
    flash[:success] = "Micropost deleted!"
        redirect_to root_url    
end

private

    def micropost_params
        params.require(:micropost).permit(:content)
    end

    def correct_user
        @micropost = current_user.microposts.find_by(id: params[:id])
        redirect_to root_url if @micropost.nil?
    end
end
class MicropostsController
@feed\u需要在控制器中的某个位置定义项目
@
是Ruby中的一个特殊符号,表示当前类的实例变量。如果在其他地方定义它,它将成为该类的实例变量

Rails有一些特殊的魔力,使控制器的实例变量在视图中可用。如果它不是控制器上的实例变量,那么它将无法工作

def create
    @micropost = current_user.microposts.build(micropost_params)
    if @micropost.save
        @feed_items = @micropost.do_whatever_to_build_the_feed_items
        respond_to do |format|
            format.html { redirect_to root_url }
            format.js 
        end
    else
        @feed_items = []
        flash[:error] = "Failed to create micropost!"
        render 'static_pages/home'
    end
end

你能粘贴控制器吗?@matanco补充道,我知道控制器正在工作,因为发布帖子时其他元素会更新。
@feed\u items
无论如何都是空的。也许这会引起你的问题。我看不出你在这个变量中加入了什么东西。@matanco不是,它是由当前的用户.feed定义的,它是microsop。从用户开始,然后是(self),我真的认为我的问题在于我如何传递部分渲染。你的解释更有意义,错误是显而易见的!谢谢!