开发者

rails url rewriting

I have a controller p开发者_运维问答erson with an action searchPerson which takes a name as parameter and redirects to the person details if it founds the person else it renders the searchPerson.html.erb with an error message.

I would like to always have http://localhost/person instead of http://localhost/person/searchPerson

so I added a route

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

so when I type http://localhost/person I can see the page searchPerson.html.erb but when I perform a search it renders searchperson and the url becomes http://localhost/person/searchPerson

my function 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

How to avoid that? thanks


redirect_to actually redirects to another url. Instead, why don't you render the details and the search in the same view ? Put the result of the search in @person. In your view, check if @person is nil and present the appropriate message.

Also, you might want to start with the basic Rails scaffolding. It does not cover search, but it is a good place to start.


try the following

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜