我是Rails的新手。我希望如果一個人在表單上輸入「entity1」(或下面代碼中名爲ENTITIES
的其他實體),表單應顯示其關係。檢查實體是否包含在實體列表中的任務是成功的,我無法理解如何打印名爲RELATION_SHIPS
的關係。我想:我在通過Rails中的控制器打印某些數據時遇到了一些問題
#puts EntityList::RELATION_SHIPS[params[:user][:entity_name]]
但它給我一個錯誤,指出模板丟失或RELATION_SHIPS
是無法識別的。我該如何解決這個問題?
#users_controller.rb
class EntityList
ENTITIES = ['entity1','entity2','entity3']
#entity1 = {:name => 'Entity One',:position => 1 :relationships => 'entity2', 'entity4'}
RELATION_SHIPS = {:entity1 => "entity2", :entity2 => "entity3"}
end
class UsersController < ApplicationController
layout 'admin'
#require File.expand_path('././myconfig') #=> C:/ruby/require/expand_path/ok.rb loaded
def new
@user = User.new
end
def create
if EntityList::ENTITIES.include?(params[:user][:entity_name])
flash[:notice] = "The entity you entered is valid!!"
redirect_to(:action => "helloworld")
#puts EntityList::RELATION_SHIPS[params[:user][:entity_name]]
else
redirect_to(:action => "SorryPage")
end
end
end
沒有我評論上述兩行Flash和重定向當我測試#puts EntityList :: RELATION_SHIPS [params [:user] [:entity_name]] – 2012-07-24 12:01:40
如果你有redirect_to,你必須有一個helloworld模板。如果你沒有它,你必須創建一個模板(這裏是:app/views/users/create.html.erb)。 – Dougui 2012-07-24 12:10:04
不正確。 redirect_to不 - 不能 - 導致創建函數返回。 – Iain 2012-07-24 12:28:29