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
4

1 回答 1

1

是的,您将创建一个 create.html.erb 并可以从那里访问所有控制器实例变量,例如 @entity 或 @var。

我建议查看生成脚手架调用的输出以获取其工作原理的示例,例如通过在新的 rails 应用程序中调用它:

rails new tryout
cd tryout
rails generate scaffold User name:string email:string

然后查看生成的控制器并查看模板。

于 2012-07-25T11:27:22.890 回答