简体   繁体   中英

Drupal-like routing system in Rails

I am trying to find a best-practice to allow users to define the route to their pages in Rails, by writing them in a text field when submitting posts, like with the Path module in Drupal (yes, we are porting a Drupal site to Rails)

So, I need to

  • define a new, named route on article submission (eg http://www.domain.com/a-day-in-annas-life )
  • change the existing route on article edit, if they define a new one, by doing a 301 redirect from the old route to the new one

How can I best achieve this?

Okay, I found a way, but if it's best practice or not, I cant say.

I am using custom restrictor's like this:

  class CharitiesRestrictor
    def self.matches?(request)
      slug = request.path_parameters[:path]
      !Charity.find_by_name(slug).nil?
    end
  end

  constraints CharitiesRestrictor do
    match  '*path' => 'charities#show_by_slug', :constraints => CharitiesRestrictor.new
  end

When I create a block like this for each of my model/controller pairs that should be able to respond to permalinks, I can have them all have a chance to act on a permalink. However, this also means that they all get called in series, which is not necessarily ideal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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