I tried to use a variable in HTML which is calculated from ruby code and MSSQL
This is my Ruby code to get @result
class StartingController < ApplicationController
before_action :require_user, only: [:index, :start]
attr_reader :table
attr_writer :table
def initialize
@table = Hash.new()
@connection = ActiveRecord::Base.connection
@st='exec search '
end
def start
.... some code set @st values
@result = @connection.exec_query(@st)
@table = @result[0]
redirect_to '/results'
end
end
def index
end
def results
end
end
This is the HTML which need to use @result
<% @result.each do |x| %>
<tr>
s: <td><%= x %></td>
</tr>
<% end %>
But I always get
undefined method `each' for nil:NilClass
if I
puts @result
I can get correct value
Can anyone help?
initializemethod, and there's no need to use ActiveRecord::Base directly if you have a model. What reference are you basing this on? A good reference book on Rails should show you how to construct this properly.initializemethod. Also, it looks like you are being redirected to"/results", I would either have your@resultsvariable being defined in theresultsaction, OR change yourredirect_to '/results'torender :results.