【发布时间】:2010-12-20 02:50:41
【问题描述】:
我有一个控制器人员,其操作为 searchPerson,它以名称作为参数,如果找到该人员,则重定向到人员详细信息,否则它会呈现带有错误消息的 searchPerson.html.erb。
我希望始终使用http://localhost/person 而不是http://localhost/person/searchPerson
所以我添加了一条路线
map.connect "person/", :controller => 'person', :action => 'searchPerson'
所以当我输入 http://localhost/person 时,我可以看到页面 searchPerson.html.erb 但是当我执行搜索时,它会呈现 searchperson 并且 url 变为 http://localhost/person/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
如何避免这种情况? 谢谢
【问题讨论】:
标签: ruby-on-rails url-rewriting routes