I have a controller for a product, and I want the actions to reflect
the different models which are numbers. Simply using a number for the
action name doesn’t seem to work:
class ClickController < ApplicationController
def index
end
def 100
end
def 200
end
end
I also have this named route in my routes file:
map.click ‘/products/click/:action’, :controller => ‘click’
If I have a non-numeric value for action, this works fine. I use it
for a different product (“command_path(:datacollector))”–which
properly generates “/products/command/datacollector”). In Rails 1.2.x
I was able to prepend the numeric action name with an underscore
(i.e., _100, _200, etc.) and everything worked as expected. In Rails
2.0 this no longer works. Someone suggested I create a custom route:
map.click ‘/products/click/:model’, :controller => ‘click’, :action =>
‘dispatch’, :requirements => {:model => /\d{3}/}, :model => nil
I have a dispatch action in my controller:
def dispatch(model = nil)
if model.nil?
index
render(:action => ‘index’)
else
# whatever
end
end
The else statement NEVER executes. I have tried the following paths:
/products/click
/products/click/100
/products/click/200
Every path renders the index action.
What am I missing? What can I do?
Thanks.
On 17 Jan 2008, at 01:07, partydrone wrote:
I have a controller for a product, and I want the actions to reflect
the different models which are numbers. Simply using a number for the
action name doesn’t seem to work:
actions are methods, method names can’t start with a digi.
map.click ‘/products/click/:model’, :controller => ‘click’, :action =>
‘dispatch’, :requirements => {:model => /\d{3}/}, :model => nil
Do you need the :model => nil ?
The else statement NEVER executes. I have tried the following paths:
/products/click
/products/click/100
/products/click/200
model should materialize as params[:model], not as an argument to your
dispatch method.
Fred
“model should materialize as params[:model], not as an argument to
your dispatch method.”
Silly me. That was my mistake.
Thanks.
Next question is how do I dynamically execute an action? Here’s what I
mean: I have my dispatch action. If :model is nil, I execute then
render the index action:
def dispatch
if params[:model].nil?
index
render(:action => ‘index’)
else
action = “click_#{params[:model]}”
# execute action
render(:action => action)
end
end
At the moment, I’m setting some variables in my action that aren’t
available in my view. How do I get them there?
Thanks.
On 17 Jan 2008, at 01:46, partydrone wrote:
action = "click_#{params[:model]}"
# execute action
render(:action => action)
end
end
You just need to call the method (eg via send)
Fred