Pi Horse Pi Horse - 4 months ago 14
Ruby Question

passing an array from controller to view in ruby

I have the following code in my Ruby Controller :

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 ???

Answer

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 %>