此代码正常运行。问题是所有东西都被打印到控制台。我想在浏览器上显示它。怎么做?我是否需要名为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
答案 0 :(得分:1)
是的,您将创建一个create.html.erb,并可以从那里访问所有控制器实例变量,如@entity或@var。
我建议查看生成脚手架调用的输出,以获得一个如何工作的示例,例如:通过在新的rails应用程序中调用它:
rails new tryout
cd tryout
rails generate scaffold User name:string email:string
然后查看生成的控制器和视图模板。