0
此代碼運行正常。問題是一切都被打印到控制檯。我想在瀏覽器上顯示它。怎麼做?我需要一個名爲create.html.erb的模板嗎?如何訪問變量和基本上在視圖中的整個控制器代碼?請幫忙!!結果輸出到控制檯而不是瀏覽器。
require File.join(Rails.root, 'config/myconfig')
puts Rails.root
class UsersController < ApplicationController
layout 'admin'
#require File.expand_path('././myconfig') #=> C:/ruby/require/expand_path/ok.rb loaded
def list
@users = User.all
end
def new
@user = User.new
end
def create
if EntityList::ENTITIES.include?(params[:user][:entity_name])
puts " Entered entity is: "
@entity = params[:user][:entity_name]
@var = EntityList::RELATION_SHIPS[@entity.to_sym]
puts "Entered entity is related to"
if(@var.nil?)
#do nothing
else
puts @var
checking(@var)
end
#@var.split(" ")
#@len = @var.length
#puts @var[0]
#puts @var[1]
#puts @var[1]
#@var.each {|@var| puts @var}
#for index in 0 ... @var.size
# puts EntityList::RELATION_SHIPS[@var[index].to_sym]
# end
# @var.each_with_index {|val, index| puts "#{val} => #{index}" }
#@var2= EntityList::RELATION_SHIPS[@var.to_sym]
#puts "Entity2 is related to"
#puts @var2
flash[:notice] = "The entity you entered is valid!!"
puts "Before Redirection"
redirect_to(:action => "helloworld")
puts "After redirection"
puts "done"
else
redirect_to(:action => "SorryPage")
end
end
def checking(array)
array.split(" ")
for index2 in 0 ... array.size
if EntityList::RELATION_SHIPS[array[index2].to_sym].nil?
# do nothing
else
puts EntityList::RELATION_SHIPS[array[index2].to_sym]
some = EntityList::RELATION_SHIPS[array[index2].to_sym]
checking(some)
end
end
end
end
非常感謝,我會閱讀這段代碼。但問題是我沒有從數據庫訪問這些數據。但是,我已經在一個名爲「myconfig.rb」的文件中定義了一個數組,這就是爲什麼我在這裏需要user_controller。所以基本上我知道在瀏覽器上顯示數據庫數據時的CRUD技術。但是,在這裏,我幾乎想要創建和檢查方法的全部內容,即要在瀏覽器上打印的內容。 :O – 2012-07-25 11:41:19
但是對於控制器視圖通信,控制器中的數據檢索方式並不重要。 (我會考慮將它移動到某個模型,儘管它處理文件讀取) – bento 2012-07-25 16:15:21