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
357 views
in Technique[技术] by (71.8m points)

ruby - Dynamic routes with Rails 3

I have a task to develop a rails application following the model for routing.

I need to have PageController and Page model. Page urls must be like /contacts, /shipping, /some_page.

Also i need have CatalogController and Category model. Categories urls must be like /laptops, /smartphones/android.

And it will be ProductsController and Product model, urls of products must be line /laptops/toshiba_sattelite_l605, /smartphones/android/htc_magic

I understand that this problem can be solved by using URLs like

  • /page/shipping
  • /catalog/smartphones/android

But the customer does not want to see the insertion of "/page" or "/catalog" in the URL.

Please tell me the direction for solving this problem. Sorry for my bad English.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

You'll have to write a "catch-all" rule:

On routes.rb:

get '*my_precioussss' => 'sauron#one_action_to_rule_them_all'

Your precious controller:

class SauronController < ApplicationController
  def one_action_to_rule_them_all
    @element = find_by_slug(params[:my_precioussss])
    render @element.kind # product, category, etc
  end
end

Then you write one view for each "kind" of element: product.html.erb, category.html.erb, etc.

Make sure you write your find_by_slug implementation.

You can change one_action_to_rule_them_all to pikachu_i_choose_you and SauronController to PokemonController, will work too.


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

...