Javascript AJAX Fave按钮无法正常工作

Javascript AJAX Fave按钮无法正常工作,javascript,ruby-on-rails,ajax,Javascript,Ruby On Rails,Ajax,我试着让用户浏览最喜欢的帖子,然后通过AJAX向他们展示某种交互,但这不起作用 我在控制台中遇到的错误是: ActionView::Template::Error (undefined local variable or method `post_item' for #<#<Class:0x007fecb2a3d5f8>:0x007fecb2a357e0>): ActionView::Template::Error(未定义的局部变量或#的“post_item”方法):

我试着让用户浏览最喜欢的帖子,然后通过AJAX向他们展示某种交互,但这不起作用

我在控制台中遇到的错误是:

ActionView::Template::Error (undefined local variable or method `post_item' for #<#<Class:0x007fecb2a3d5f8>:0x007fecb2a357e0>):
ActionView::Template::Error(未定义的局部变量或#的“post_item”方法):
按钮将通过局部渲染进行渲染:

<%= render "shared/fave_form", post_item: post_item %>

下面是按钮的代码(shared/\u fave\u form.html.erb):


true,:method=>:post,:class=>“btn”)%>
true,:method=>:post,:class=>“btn”)%>
下面是toggle.js.erb文件:

 $("#fave").html("<%= escape_javascript render('fave_form') %>");
$(“#fave”).html(“”);

该部分正在使用局部变量,因此将
post\u项作为局部变量传递:

<%= render :partial => "shared/fave_form", :locals => {post_item: post_item} %>
“共享/喜爱表单”:locals=>{post\u item:post\u item}%>

当您使用
toggle.js.erb
渲染部分时,它没有获得
局部变量
post\u项
,您还必须在中提供它。因此,您的js代码应该如下所示

$("#fave").html("<%= escape_javascript(render :partial=>"fave_form", locals: {post_item: post_item}).html_safe %>);

仍然得到相同的东西。我仍然在控制台中得到相同的错误?我使用了你的代码,它产生了同样的结果。出于某种原因,它仍然不起作用?也许控制器里有什么东西?
$("#fave").html("<%= escape_javascript(render :partial=>"fave_form", locals: {post_item: post_item}).html_safe %>);
$("#fave").html("<%= escape_javascript(render :partial=>"fave_form", locals: {post_item: @post_item}).html_safe %>);