在ruby中,将数组从控制器传递到视图

时间:2022-12-01 21:28:32

I have the following code in my Ruby Controller :

我的Ruby控制器中有如下代码:

mastertest_controller.rb

mastertest_controller.rb

def index

 ......

 @mastertest = connection.execute("select code_ver from mastertest")
 result_array = { sometihng }

 ......

 respond_to do |format|
  format.html # index.html.erb
  format.json { render :json => @mastertest}
  format.json { render :json => result_array}

 end

But it just allows me to access @mastertest in the view(index.html.erb). How do I pass the array to the view ???

但它只允许我访问视图中的@mastertest (index.html.erb)。如何将数组传递给视图?

1 个解决方案

#1


8  

instance variables in your controller are passed as instance variables in your view.

控制器中的实例变量在视图中作为实例变量传递。

Controller:

控制器:

@result_array = [something, second_something]

Then in your view:

然后在你的观点:

<% @result_array.each do |item| %>
  <%= item %>
<% end %>

#1


8  

instance variables in your controller are passed as instance variables in your view.

控制器中的实例变量在视图中作为实例变量传递。

Controller:

控制器:

@result_array = [something, second_something]

Then in your view:

然后在你的观点:

<% @result_array.each do |item| %>
  <%= item %>
<% end %>