views:

26

answers:

2

I am trying to link to action addData in the entries controller. I have constructed the link like this:

<%= link_to image_tag (w.link, :border =>0) ,:controller => :entries, :action  =>  :addData %>

but when I click on the link, I get this error:

Couldn't find Entry with ID=addData

I'm pretty sure this is because I have a restful design. Is there a way around this problem? Thanks for reading.

+1  A: 

Have you defined the route properly for this action addData? By the way try this :

<%= link_to image_tag (w.link, :border =>0) ,{:controller => :entries, :action  =>  :addData} %>
Suman Mukherjee
Ah I didn't know I had to define a route. So I can't just link like this without defining a route?
ben
Routes are basically nothing but regular expressions to which the relative part of the url is mapped. The mapping starts from the top of the routes file. So, if it encounters any regular expression to which this url can be mapped, it will treat it in that format. So, the map.resources part should come on top and map.connect part ( the default format ) should come below. If it does not find any specified route for the url, it will try mapping it to the default format (i.e, the map.connect part). Hope the explanation helps.
Suman Mukherjee
+1  A: 

Rails has migrated wholly to a RESTful design. This means that in order to use non standard actions you have to add them to your resources in config/routes.rb.

If they operate on all resources you add them to the hash :collection => {:addData => :post} In case you have one operating on a single resource with an id use :member. To some it up for you.

map.resources :entries, :collection => {:addData => :post}

To use the old style of mapping any action to any controller you can add the following two lines to your config/routes.rb

  map.connect ':controller/:action/:id.:format'
  map.connect ':controller/:action/:id'
nasmorn