【问题标题】:rails url rewritingrails url重写
【发布时间】: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


    【解决方案1】:

    redirect_to 实际上重定向 到另一个 url。相反,为什么不在同一个视图中呈现细节和搜索呢?将搜索结果放在@person 中。在您看来,检查 @person 是否为 nil 并显示适当的消息。

    另外,您可能想从基本的 Rails scaffolding 开始。它不包括搜索,但它是一个很好的起点。

    【讨论】:

      【解决方案2】:

      试试下面的

      redirect_to :controller => 'person', :action => nil, :id => nil

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-17
        • 1970-01-01
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-01
        相关资源
        最近更新 更多