The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Add routes with a rails plugin or gem

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Paul Gross

Posts: 152
Nickname: pgross
Registered: Sep, 2007

Paul Gross is a software developer for ThoughtWorks.
Add routes with a rails plugin or gem Posted: Sep 28, 2007 1:44 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Paul Gross.
Original Post: Add routes with a rails plugin or gem
Feed Title: Paul Gross's Blog - Home
Feed URL: http://feeds.feedburner.com/pgrs
Feed Description: Posts mainly about ruby on rails.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Paul Gross
Latest Posts From Paul Gross's Blog - Home

Advertisement

It is possible to define routes in a ruby on rails plugin or gem. Normally, adding routes looks like this:


ActionController::Routing::Routes.draw do |map|
  map.connect ':controller/:action/:id'
end

The problem is that the draw method clears the existing routes before adding the new ones (Ruby on Rails 1.2.3: routing.rb):


def draw
  clear!
  yield Mapper.new(self)
  named_routes.install
end

The plugins and gems are loaded first, so any new routes are cleared when config/routes.rb is loaded. One solution is to redefine the clear! method to do nothing:


class << ActionController::Routing::Routes;self;end.class_eval do
  define_method :clear!, lambda {}
end

The final result should be included in the plugin or gem:


class << ActionController::Routing::Routes;self;end.class_eval do
  define_method :clear!, lambda {}
end

ActionController::Routing::Routes.draw do |map|
  map.connect 'newurl', :controller => 'plugin_controller', :action => 'some_action'
end

Read: Add routes with a rails plugin or gem

Topic: Ruby constants have weird behavior in class_eval Previous Topic   Next Topic Topic: Advice For Ruby Beginners 1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use