# Numerical Action Names

**URL:** <https://www.ruby-forum.com/t/numerical-action-names/126292>\
**Category:** Rails\
**Created:** [January 17, 2008, 2:08am UTC](https://www.ruby-forum.com/t/numerical-action-names/126292 "2008-01-17T02:08:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [January 17, 2008, 2:08am UTC](https://www.ruby-forum.com/t/numerical-action-names/126292/1 "2008-01-17T02:08:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [January 17, 2008, 2:23am UTC](https://www.ruby-forum.com/t/numerical-action-names/126292/2 "2008-01-17T02:23:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [January 17, 2008, 2:24am UTC](https://www.ruby-forum.com/t/numerical-action-names/126292/3 "2008-01-17T02:24:32Z")

</div>

“model should materialize as params[:model], not as an argument to  
your dispatch method.”

Silly me. That was my mistake.

Thanks.

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [January 17, 2008, 2:56am UTC](https://www.ruby-forum.com/t/numerical-action-names/126292/4 "2008-01-17T02:56:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![partydrone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a6a055/32.png) [@partydrone](https://www.ruby-forum.com/u/partydrone)\
**Post date:** [January 17, 2008, 11:31am UTC](https://www.ruby-forum.com/t/numerical-action-names/126292/5 "2008-01-17T11:31:01Z")

</div>

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
