2010-06-15 28 views
0

我有一個關於導軌一個小問題。我有一個搜索控制器,其搜索數據庫的名稱,如發現顯示有關它的細節,否則我重定向到新名稱的頁面。是否有名稱搜索到自動出現在新的表單頁面重定向後呢?重定向Rails中

在此先感謝。

回答

2

可以使用ActionController::Flash到行動之間傳遞數據。

def search(searchedName) 
    # perform search on DB 

    flash[:searchedName] = searchedName 

    redirect_to new_name 
end 

def new_name 
end 

new_name.html.erb:

<% if flash[:searchedName] %> 
    <%= text_field_tag flash[:searchedName] %> 
<% end %> 
+0

尼斯。但是,我如何在下一頁的特定文本字段中設置它? – 2010-06-15 01:15:29

+0

@Cyborgo:添加代碼段查看設置文本框內容與傳遞的數據 – Anero 2010-06-15 01:31:00

+0

非常感謝您的幫助! :) – 2010-06-15 20:09:20

2

好吧,讓我們說你要保存用戶對一個變量命名@name進入這個 '名'。
所以,這做搜索的動作控制器上,你不喜歡的東西:

if @name 
    ...#action if the name was found 
else 
    session[:name] = @name 
    ...#here is the action you did to redirect 

在調用該方法的聲明(你說,這是「新」):

def new 
    @name = session[:name] if session[:name] #I don't know exactly if the condition is this one, 
              #but you have to confirm that the session was instatiated 
    session.delete(:name) #to don't let garbage 
    ...#continuous the action of show the new page 
    #and on the page you have access to a variable @name, that have the name searched before. 
+0

我相信這只是'如果會話[:名字]' – 2010-06-15 02:19:38