将实例变量传递给js.erb文件(Rails 3 / jQuery) [英] Passing instance variable to js.erb file (Rails 3 / jQuery)

查看:90
本文介绍了将实例变量传递给js.erb文件(Rails 3 / jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有以下内容的index.html.erb文件:

I have an "index.html.erb" file with the following:


<%= render @users%>

<%= render @users %>

这会呈现_user.html.erb并输出一个按钮,用于对每个用户执行某项操作:

This renders "_user.html.erb" and outputs a button for performing a certain action on each user:


<%= link_toaction,action_path(user),:id =>#{user.id} _action,:remote => true) %>

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

我设置了我的用户控制器,通过查看action.js.erb来响应AJAX请求。

I've set up my User controller to respond to the AJAX request by looking at "action.js.erb".

为了对partial中的特定用户执行javascript方法,我想知道我的部分(例如user.id)中的实例变量是怎样的传入或访问js.erb文件,例如:

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:

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


推荐答案

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

You access the information in your *.js.erb-files just like in your *.html.erb-files, using @users or other instance variables declared in the controller:

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

这篇关于将实例变量传递给js.erb文件(Rails 3 / jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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