如何在Javascript代码中访问rails变量?

时间:2022-01-07 20:46:33

I am using Rails 3.0.5

我正在使用Rails 3.0.5

I have a javascript code that uses jQuery dropdown checklist and I would like to have the checkboxes displaying which item is checked already and which is not, when using my form in the 'Edit' action of my belongings controller.

我有一个使用jQuery下拉清单的javascript代码,我希望在我的财物控制器的“编辑”操作中使用我的表单时,复选框显示已经检查了哪个项目,哪个不是。

Basically, it can work like this in Javascript:

基本上,它可以在Javascript中这样工作:

$('element').val([1,3,4]);
$('element').dropdownchecklist();

Let us say that in my controller, edit action, I have a instance variable called @selected_items that contains the items that are selected (here: @selected_items = [1,3,4]) and I would like to use the value of this instance variables into my Javascript code, how could I proceed ? How could I access the value of this instance variable in a .js file ?

让我们说在我的控制器中,编辑动作,我有一个名为@selected_items的实例变量,其中包含所选项(这里:@selected_items = [1,3,4]),我想使用这个值实例变量到我的Javascript代码中,我该怎么办?我怎么能在.js文件中访问这个实例变量的值?

Many thanks for your answer !

非常感谢您的回答!

2 个解决方案

#1


7  

Just write the javascript in your view, and at the appropriate place, print the @selected_items array as json:

只需在视图中编写javascript,并在适当的位置打印@selected_items数组作为json:

app/views/*/*.html.erb

<script>
  $('element').val(<%= @selected_items.to_json %>);
  $('element').dropdownchecklist();
</script>

You can also use the Gon gem if to_json cannot serialize more complicated objects.

如果to_json无法序列化更复杂的对象,也可以使用Gon gem。

#2


1  

A better and a clean/neat way of handling such requirements is

处理这些要求的更好和干净/整洁的方法是

1.Don't create javascript inside the views not as per the rails best practices

1.不要根据rails最佳实践在视图中创建javascript

2.Handle it this way

以这种方式处理它

In your app/views/*/*.html.erb where you are having the instance variable like (@selected_items) assign it into the options within the helpers something like :sel => @selected_items

在您的app / views / * / * .html.erb中,您正在使用类似(@selected_items)的实例变量,将其分配给帮助程序中的选项,例如:sel => @selected_items

In your javascript

在你的JavaScript中

<script>
  $('element').attr('sel');
  $('element').dropdownchecklist();
</script>

I hope it helps!

我希望它有所帮助!

#1


7  

Just write the javascript in your view, and at the appropriate place, print the @selected_items array as json:

只需在视图中编写javascript,并在适当的位置打印@selected_items数组作为json:

app/views/*/*.html.erb

<script>
  $('element').val(<%= @selected_items.to_json %>);
  $('element').dropdownchecklist();
</script>

You can also use the Gon gem if to_json cannot serialize more complicated objects.

如果to_json无法序列化更复杂的对象,也可以使用Gon gem。

#2


1  

A better and a clean/neat way of handling such requirements is

处理这些要求的更好和干净/整洁的方法是

1.Don't create javascript inside the views not as per the rails best practices

1.不要根据rails最佳实践在视图中创建javascript

2.Handle it this way

以这种方式处理它

In your app/views/*/*.html.erb where you are having the instance variable like (@selected_items) assign it into the options within the helpers something like :sel => @selected_items

在您的app / views / * / * .html.erb中,您正在使用类似(@selected_items)的实例变量,将其分配给帮助程序中的选项,例如:sel => @selected_items

In your javascript

在你的JavaScript中

<script>
  $('element').attr('sel');
  $('element').dropdownchecklist();
</script>

I hope it helps!

我希望它有所帮助!