2013-11-02 98 views
6

我想定制我的表演路线......我不希望用户能够通过查看物品:身份证...导轨 - 自定义资源路线

我有一种销售模式,以及显示的路线是:

sale GET /sales/:id(.:format)  sales#show 

不过,我不希望用户能够通过ID查看销售,相反,我想它是:

sale GET /sales/:guid(.:format)  sales#show 

GUID是一个UUID我创建对象时生成:

def populate_guid 
    self.guid = SecureRandom.uuid() 
end 
+0

什么是':guid',一些替代的ID? – jvperrin

+0

这是我生成的UUID(在原文中增加了更多信息) – cmw

回答

1

你可以在你的routes.rb定义自定义路线:

get "/sales/:guid", :to => "sales#show" 

然后在您的控制器,为展会的动作,你会发现从已在通过​​你想出售网址:

def show 
    @sale = Sale.find_by_guid(params[:guid]) 
end 
9

在配置/ routes.rb中

get '/sales/:guid', to: 'sales#show' 

或者,如果你使用的轨道4,您可以:

resources :sales, param: :guid 

在控制器

def show 
    @sale = Sale.find(params[:guid]) 
end 
+0

我喜欢铁轨4种方式!谢谢 – brookz