# Route not generated, what have I missed?

**URL:** <https://www.ruby-forum.com/t/route-not-generated-what-have-i-missed/175253>\
**Category:** Rails\
**Created:** [September 10, 2009, 9:20am UTC](https://www.ruby-forum.com/t/route-not-generated-what-have-i-missed/175253 "2009-09-10T09:20:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [September 10, 2009, 9:20am UTC](https://www.ruby-forum.com/t/route-not-generated-what-have-i-missed/175253/1 "2009-09-10T09:20:20Z")

</div>

Hey Guys,  
I’m basing this off of the book Simply Rails 2 by Patrick Lenz.  
I’ve got my word model:

# word.rb

has\_many :votes

my vote mode;

# vote.rb

belongs\_to :word, :counter\_cache =\> true

my routes

# routes.rb

map.resources :words, :has\_many =\> :votes

Then my problem. In my view I’m trying to use a generated url for a  
form button to create a vote

# index.html.erb (in the words folder)

\<% @words.each do |word| %\>

 \<%=h word.word %\> \<%=h word.user\_id %\> \<%=h word.votes\_count %\> \<%= link\_to 'Show', word %\> \<%= link\_to 'Destroy', word, :confirm =\> 'Are you sure?', :method =\> :delete %\> 
 \<% form\_tag :url =\> word\_votes\_path do %\> \<%= submit\_tag 'shove it' %\> \<% end %\> 
 \<% end %\>

word\_votes\_url failed to generate from  
{:action=\>“index”, :controller=\>“votes”} - you may have ambiguous  
routes, or you may need to supply additional parameters for this  
route. content\_url has the following required parameters:  
[“words”, :word\_id, “votes”] - are they all satisfied?

I was under an impression having the has many and belongs to  
relationships in the model, as well as the defined route would  
generate the words\_votes\_path for me. It works in the book example and  
I can’t find any other defining attributes that I’m lacking but i must  
be missing something.

Thanks,  
I was so confident after I went through the multiple demos of rails.  
But seem to be catching snags on much more then expected on my own  
project.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [September 10, 2009, 9:32am UTC](https://www.ruby-forum.com/t/route-not-generated-what-have-i-missed/175253/2 "2009-09-10T09:32:03Z")

</div>

word\_votes\_url needs word id to generate the url, therefore  
{:action=\>“index”, :controller=\>“votes”} alone is not sufficient. You  
need to pass word id as well.

See rake routes for all the paths/url generated and ids they need.

Thanks,  
Abhinav

–  
à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [September 10, 2009, 10:37am UTC](https://www.ruby-forum.com/t/route-not-generated-what-have-i-missed/175253/3 "2009-09-10T10:37:02Z")

</div>

Brilliant!  
I don’t know why but what you said just made it click for me.

I changed my form to:  
\<% form\_tag word\_votes\_path :word\_id =\> word.id do %\>  
\<%= submit\_tag ‘shove it’ %\>  
\<% end %\>

The votes get created and the counter\_caches work like expected. The  
only other thing i notice is the ugly action method it creates:

where as the simply rails demo produced:

I realize the major difference is the use of form\_remote\_tag which  
creates the ajax but thats not really the concern. Just the better  
looking action url

In theory couldn’t I get mine to look something like:  
/word/2/votes

?  
Thanks for the info so far.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [September 10, 2009, 10:54am UTC](https://www.ruby-forum.com/t/route-not-generated-what-have-i-missed/175253/4 "2009-09-10T10:54:36Z")

</div>

Hi Brian,

You can modify your code like this as well:  
\<% form\_tag word\_votes\_path(word) do %\> # note word as  
argument  
\<%= submit\_tag ‘shove it’ %\>  
\<% end %\>

I am not aware of simply rails demo, but in general, word/2/votes  
corresponds to index (get) or create (post) action, and it’s not a  
good idea to change the behaviour. You can add a new action in your  
votes resource and call it something like say “count” and then submit  
your remote form to this new “count” path.

See rails routing guide to understand more about routes, and how to  
add new actions: [Rails Routing from the Outside In — Ruby on Rails Guides](http://guides.rubyonrails.org/routing.html)

## Thanks, Abhinav

à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [September 10, 2009, 6:33pm UTC](https://www.ruby-forum.com/t/route-not-generated-what-have-i-missed/175253/5 "2009-09-10T18:33:37Z")

</div>

Hey,  
Create is exactly what the form goes to now which is why I don’t get  
why it hasn’t created the word/2/votes route as create (post) is the  
desired behavior. As well I’ve tried using word\_votes\_path(word) and I  
actually receive this generated output:

\*\* Just found my problem,  
I was using map.resource :word, :has\_many =\> :votes  
but needed: map.resources :words, :has\_many =\> :votes

it now generates the correct desired url. words/1/votes

Thanks for your help Abhinav !
