link_to problem

hi @ all

I create a new archiv.html.erb and a archiv method in the controller. Now, I would like to link from index.html.erb to archiv.html.erb. I tried to use <%= link_to 'archive', archiv_xxx_path %> but it doesn't run. It appears a error message "SyntaxError in xxx#index".

Does anyone know, how I can enhance this problem?

thanks a lot...

Hi, You can use <%= link_to 'archive', :action => 'archive' %> in the view.

Then define an action in your controller. To do this, all you need is:    def archive    end This will automatically go to a page titled "archive" in your controller's view, ex: archive.rhtml

Good luck, - Jeff

K. R. wrote:

That I'm not so sure on... Is your action defined in a different controller? If so, you can use: <%= link_to 'action', :controller => 'ControllerName', :action => 'ActionName' %>

Hope it helps... Other than that, I don't know what that error is...

K. R. wrote:

No, both methods are in the same controller - so I don't need the :controller statement.

In the new version of rails (2.0), I can use for example <%= link_to 'show', show_xxx_path %>. Why can't I use the same principle with the archive? I want to use <%= link_to 'archive', archiv_xxx_path %> but it doesn't run!

Jeff Miller wrote:

hmmm... I'm pretty new at RoR, so I'm not sure about this, but have you tried naming it something other than "archive"? Perhaps it is a built-in method or class or something that is throwing off your statement. Try "archive_data" or something.

K. R. wrote:

sorry, but it isn't a problem with the naming convention. When I put an id behind the url, then the application runs. For example: http://localhost:3000/xxx/archive/1 but with http://localhost:3000/xxx/archive it appears a error message. So I must change the routes.rb but I don't know how... Now when I use http://localhost:3000/xxx/archive the interpreter means, that archive is an id and not a method.

Jeff Miller wrote:

Hi K. R.,

The record not found error is due to the RESTful interpretation of URLs by Rails. Given the URL ...

http://localhost:3000/xxx/archive

... Rails rails assumes that you are calling the 'show' action on the 'xxx' controller with 'archiv' as the ID of the model instance to be shown.

To get rails to understand that 'archiv' is a new action, you need to edit the routes.rb file and add appropriate routes using either the :collection option (if the archiv action works with a collection of model instances) or with the :member option (if you are working with only one instance of the model).

Check out the screencasts below for a quick intro to routes and custom actions

Named routes

Custom REST actions

Cheers, Akbar

You need to specify that your resource have a new method, i.e :

  map.resources user do |users|      user.resources :tournament, :member=>{:stats=>:get }   end

  And now you can request the url "/users/:user_id/tournaments/:tournament_id/stats" with stats_user_tournament(@user, @tournament)