0
我試圖使用HTML這是從Ruby代碼和MSSQL如何把變量轉換成HTML從Ruby on Rails的
計算的變量這是我的Ruby代碼來獲得@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
這是需要@result
<% @result.each do |x| %>
<tr>
s: <td><%= x %></td>
</tr>
<% end %>
使用,但我總是得到
undefined method `each' for nil:NilClass
的HTML
如果我
puts @result
我能得到正確的值
誰能幫助?
我不確定這裏發生了什麼。控制器不應該有'initialize'方法,並且如果有模型,則不需要直接使用ActiveRecord :: Base。你基於什麼參考?關於Rails的一本很好的參考書應該告訴你如何正確地構造這個。 – tadman
我同意@tadman,對於你的控制器來說''initialize'方法不是一個好主意。另外,它看起來像被重定向到''/ results'',我要麼在'results'動作中定義您的'@ results'變量,要麼將'redirect_to'/ results''更改爲'render: results'。 – sclem72