简体   繁体   English

如何更新routes.rb(动态网址)

[英]How to update routes.rb (dynamic urls)

I have this code in my routes.rb 我的routes.rb中有此代码

shops = Shop.all
shops.each do |shop|
match "/#{shop.url}" => 'shops#show', :id => shop.id
end

So url can be like http: //site/url & not like http: //site/shops/1 因此url可以像http:// site / url而不是http:// site / shops / 1

& It does work, but I have to restart server after adding a new shop. &它确实可以工作,但是我必须在添加新商店后重新启动服务器。

Maybe, there is a way to do this stuff without restarting? 也许有一种方法可以在不重新启动的情况下执行此操作? Or, some other way around? 还是其他方式?

Thank you 谢谢

This is an old rails cast, but you'll find elements of answer there 这是一个古老的方法,但是在那里您会找到答案的要素

http://railscasts.com/episodes/63-model-name-in-url http://railscasts.com/episodes/63-model-name-in-url

As a general advise: you won't need do this kind of loop in your routes, study the tools made available by rails routing and use them 一般建议:您无需在路线中进行这种循环,无需研究Rails路线提供的工具并使用它们

http://guides.rubyonrails.org/routing.html http://guides.rubyonrails.org/routing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM