Rails路由/剪辑错误的网址

时间:2014-01-28 10:38:27

标签: ruby-on-rails routes url-routing

在我的路由器中我有:

get 'cars/:make', to: 'cars#make'

当人们访问/汽车/闪避或/汽车/丰田时这种方法很好,但是当人们进入/汽车/制造不存在时,我更喜欢页面掉落'make-does-not-exist'来自网址,并将它们发送到/ cars。

在我的汽车#make控制器中我有一些逻辑可以正确设置我的观点:

def make
 case params[:make]
   when "dodge"
    @text = "dodge text"
   when "toyota"
    @text = "toyota text"
   else
    @text = "default text"
    #how do I send people to /cars, and not /cars/make-does-not-exist
 end
 render :index  #render index with the @text that we just set
end

如何设置控制器的网址?

谢谢!

1 个答案:

答案 0 :(得分:0)

为了能够将人们重定向到/cars,您必须添加到路线

get 'cars', to: 'cars#make'

修改你的控制器看起来更像

def make
 case params[:make]
   when nil
    @text = "default text"
   when "dodge"
    @text = "dodge text"
   when "toyota"
    @text = "toyota text"
   else
    wrong_make = true
 end
 if defined? wrong_make
   redirect_to 'cars'
 else
   render :index
 end
end

希望有所帮助