简体   繁体   English

将实例变量传递给js.erb文件(Rails 3 / jQuery)

[英]Passing instance variable to js.erb file (Rails 3 / jQuery)

I have an "index.html.erb" file with the following: 我有一个“index.html.erb”文件,其中包含以下内容:

<%= render @users %> <%=渲染@users%>

This renders "_user.html.erb" and outputs a button for performing a certain action on each user: 这将呈现“_user.html.erb”并输出一个按钮,用于对每个用户执行特定操作:

<%= link_to "action", action_path(user), :id => "#{user.id}_action", :remote => true) %> <%= link_to“action”,action_path(user),:id =>“#{user.id} _action”,:remote => true)%>

I've set up my User controller to respond to the AJAX request by looking at "action.js.erb". 我已经设置了我的用户控制器,通过查看“action.js.erb”来响应AJAX请求。

In order to perform javascript methods on particular users within the partial, and I'd like to know how instance variables from my partial (such as user.id) can be passed into or accessed within the js.erb file, for instance: 为了在partial中的特定用户上执行javascript方法,我想知道我的部分实例变量(例如user.id)如何在js.erb文件中传递或访问,例如:

$("#{@user.id}_action").toggle();

你访问你的信息*.js.erb -files就像在你的*.html.erb -files,使用@users或控制器中声明的其他实例变量:

$("#<%= @user.id %>_action").toggle();

有一个很棒的宝石叫gon,它可以简化和组织一些东西: https//github.com/gazay/gon/blob/master/README.md

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM