AJAX按钮不工作Rails
AJAX Fave Button Not Working Rails
我试图让用户喜欢的帖子,然后它通过AJAX向他们展示某种交互,但它不工作。
我在控制台得到的错误是:
ActionView::Template::Error (undefined local variable or method `post_item' for #<#<Class:0x007fecb2a3d5f8>:0x007fecb2a357e0>):
按钮正在通过局部渲染:
<%= render "shared/fave_form", post_item: post_item %>
下面是按钮(shared/_fave_form.html.erb)的代码:
<% if current_user.voted_on?(Post.find(post_item)) %>
<%= link_to "unlike", vote_against_post_path(post_item.id), :remote => true, :method => :post, :class => "btn") %>
<% else %>
<%= link_to "like", vote_up_post_path(post_item.id), :remote => true, :method => :post, :class => "btn") %>
<% end %>
这是toggle.js.erb文件:
$("#fave").html("<%= escape_javascript render('fave_form') %>");
当您使用toggle.js.erb
渲染部分时,它不会获得locals
值post_item
,您也必须提供它。所以,你的js代码应该像下面这样
$("#fave").html("<%= escape_javascript(render :partial=>"fave_form", locals: {post_item: post_item}).html_safe %>);
我猜你正在使用一些ajax调用,然后你的toggle.js.erb
,所以在你的toggle
的行动,你必须指定值post_item
,让它的实例变量@post_item
,以便我们可以使用它在toggle.js.erb
。
$("#fave").html("<%= escape_javascript(render :partial=>"fave_form", locals: {post_item: @post_item}).html_safe %>);
局部使用了一个局部变量,所以传递post_item
作为局部变量:
<%= render :partial => "shared/fave_form", :locals => {post_item: post_item} %>
相关文章:
- Rails-JQueryUIAutcomplete和AJAX不工作,可以't连接到数据库
- 引导程序转盘不工作:堆叠图像(使用rails)
- 为什么Turbolinks不能正常工作?Rails应用程序
- Rails Production-可以工作,但不能编辑/删除/创建记录
- escape_javascript无法在rails 3.2.7中工作
- 地图,rails 4.2,javascript,鼠标悬停,只工作一次(或两次).然后在重新加载之后
- Rails JQuery和Ajax对我来说工作不正常
- 为什么不是't在我的rails部分中工作的DataTables
- Rails页面JS可以在真正的浏览器/手动测试中工作,但不能在PhantomJS&硒规格
- 如何让手图在 Rails 中工作
- Rails 应用程序中不显眼的 JS 无法按预期工作
- rails 预编译 为什么 javascript 在本地不起作用,但在 heroku 它可以工作
- Rails 3.2.3 JavaScript 突然停止工作
- Javascript文件只能在Rails应用程序中按字母顺序工作
- 如何让 AJAX 使用 rails 和 JavaScript 计时器工作
- 自动关闭引导警报在 Rails 中停止工作
- Rails 4:资产管道的正常CSS/JS开发工作流程
- Rails 4 Ajax 调用只工作一次
- Rails 4 turbo-link 阻止 jQuery 脚本工作
- Rails 应用程序 - jQuery Document.ready在单击链接时不调用,但在新选项卡中打开或刷新时工作