Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
828 views
in Technique[技术] by (71.8m points)

ruby on rails - Get same url structure as on Stack Overflow

A question on SO has this url structure:

http://stackoverflow.com/questions/18474799/changing-the-input-value-in-html5-datalist

If we assume that the number section is the ID, the first two sections (after the domain extension) are obtained by simply using the following in routes.rb

resources :questions

The question is already identified by it's ID, so how do we add the (optional) decorating slug in the simplest of manners? Do we need to use a new link helper (and including additional params) or can the 3-section url be resolved elsewhere?

Update:

To focus this question more on the route-handling, let's presume there is already a slug saved on the object (upon creation) as an attribute, e.g. @question.slug

It would really be an advantage if a rule in routes.rb or/and in the controller could enable and handle the optional slug, instead of having to write long link helpers in all views.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
resources :questions do
  member: title
end

for slug use friendly_id and yes don't forget to have a look at Rails Routing


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...