How to write a dynamic route in Rails 3?

I am trying to write a generic route that will allow me to refer to it using a controller action.

I am using the following line:

  match ':action' => 'pages#:action', :as => 'action'

say an action called foobar in the page controller. I would like to write

link_to 'Click Me', pages_foobar_path

in view. The problem is that I get an error Invalid route name: ':action'when I try to record this route.

Note the line

match ':action' => 'pages#:action'

:asworking without a parameter works fine, but then I have to manually write the path as such:

link_to 'Click Me', '/pages/foobar'

in any way around this?

+3
source share
3 answers

If dynamic means "recognize my actions when Rails starts and dynamically generates routes":

, , , , time_missing.

config/routes.rb

controller_filenames = Dir.new("#{Rails.root}/app/controllers").entries
controller_filenames.each do |filename|
  # you could restrict to only the pages_controller.rb on the next line,
  # and in that case, you could simplify even more (see next example)...
  if filename =~ /_controller\.rb$/
    controller_name = filename.sub(/.rb$/, "")
    controller_route_name = controller_name.sub(/_controller$/, "")
    controller = controller_name.camelize.constantize.new
    controller.action_methods.each do |action|
      # if you don't want the controller name in your path match, just omit it...
      match "#{controller_route_name}/#{action}" => "#{controller_route_name}##{action}", :as => "#{controller_route_name}_#{action}"
    end
  end
end

pages_controller.rb, :

controller_name = "pages_controller"
controller_route_name = "pages"
controller = controller_name.camelize.constantize.new
controller.action_methods.each do |action|
  # I've removed the controller_route_name from the match here...
  match "#{action}" => "#{controller_route_name}##{action}", :as => "#{controller_route_name}_#{action}"
end

, " , " :

. . , config/routes.rb ( ):

match '/dynamic_define' => 'application#dynamic_define'

ApplicationController ( , ):

def dynamic_define
  method_name = params[:mname]
  self.class.send(:define_method, method_name) {
    render :text => "output from #{method_name}"
  }
  Rails.application.routes.disable_clear_and_finalize = true
  Rails.application.routes.draw do
    match "/#{method_name}" => "application##{method_name}", :as => "application_#{method_name}"
  end
  render :text => "dynamic_define just created a new action named #{method_name}"
end

:

/dynamic_define?mname=my_new_dynamic_action
# browser renders "dynamic_define just created a new action named my_new_dynamic_action"

:

/my_new_dynamic_action
# browser renders "output from my_new_dynamic_action"
+5

, :

link_to 'Click me', pages_path(:action)

match ':action' => 'pages#:action'
match '/pages/:action' => redirect(":action") # pages_path(:action) will match

, , , , -.

, method_missing , _ [stuff] _path , .

def method_missing(name, *args, &block)
  if name =~ /^pages_[a-z]*_path$/
    "/#{name.to_s.gsub!(/^pages_/,'').gsub!(/_path$/,'')}"
  else
    super
  end
end

, method_missing - , .

+2

If you write your route this way, the correct way to access it is:

link_to 'Click me', action_path(:action => 'foobar')
0
source

All Articles