2009-11-25 26 views
0

我有一個操作員searchPerson,它將一個名稱作爲參數,並重定向到該人員的詳細信息,如果它發現該人員,則呈現包含錯誤消息的searchPerson.html.erb。rails url rewriting

我想總是有http://localhost/person代替http://localhost/person/searchPerson

,所以我說的路線

map.connect  "person/", :controller => 'person', :action => 'searchPerson' 

所以當我輸入http://localhost/person我可以看到頁面searchPerson.html.erb 但是當我執行搜索它呈現的搜索人和url變成http://localhost/person/searchPerson

我的功能searchPerson

def searchPerson 

    flash[:error]=nil 
    @name=params[:name] 

    #if a name is provided 
    if(@name!=nil) 

     #trying to find the person with the name 
     ret = Person::lookup @name 

     #error, the person cannot be found 
     if ret[:err] 
     flash[:error]="We could not find this person" 
     #person found, user is redirected to the person details 
     else 
     redirect_to url_for(:controller => 'person', :action => 'details', :id => ret[:person].id) 
     end 
    end 
end 

如何避免這種情況? 感謝

回答

0

redirect_to的實際上重定向到另一個URL。相反,你爲什麼不在相同的視圖中呈現細節和搜索?將搜索結果放在@person中。在你看來,檢查@person是否爲零,並提供適當的信息。

此外,您可能想要從基本的Rails scaffolding開始。它不包括搜索,但它是一個開始的好地方。

0

嘗試以下

redirect_to的:控制器=> '人',:動作=>零,:ID =>零