Topic: link_to the Action i have defined

hi,

i forgot define an Action when g congtroller, so i want define a Action in the controller.rb for link_to.

i test the way of changing the route but failed.

i am new to rails and know this problem can be solved if i regenerate the controller, but how to solve this problem without regenerate controller?

look forward your answer , many thanks

i am a rails rookie

Re: link_to the Action i have defined

set up a route and add the action to the controller concerned.
Can't really say much more than that as there is no detail to work with.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: link_to the Action i have defined

jamesw wrote:

set up a route and add the action to the controller concerned.
Can't really say much more than that as there is no detail to work with.

hi jamsew,

i generate a scaffold "rails g scaffold friend name:string email:string" then i can "create friend" in index.html.erb

after create many friends

if i want to delete a friend then i can click the "destroy" behind the "friend" but how to delete all friend a time ?!

so i want to add a "delete all friends" link_to behind the "create friend" link_to and add an action "delete_all_friends" in the friends_controller.rb

how to let "delete all friends" link up with "delete_all_friends" ??

thank you very much!

i am a rails rookie

Re: link_to the Action i have defined

a delete method in rails is kinda treated as a get request but it isn't really as the linkT_ uses javascript for the delete method to fake this and as you are working on multiple records you won't be passing in an id therefore you will nee a collection route rather than a member route for your friends controller so in routes.rb change the

resources :firends

to add the delete_all route

  resources :friends do
    collection do
      get "delete_all"
    end
  end

This will give you delete_all_friends_path which you can use in your link_to helper (Run rake routes in the console and you will see the paths available to you)
Then you nee to create the def delete_all method in the friends controller and do whatever you wish with it

WARNING

As stated in my opening sentence links for delete methods are kinda faked so the solution I have given you is the answer to your question rather than the most appropriate solution as you really really should be using a PUT request instead of a get request as deleting existing records is a dangerous action and therefore you should be using a button not a link so be careful what you wish for coz you may just get it smile

To do it properly add a button_to instead of a link and change the get to a put in the routes.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: link_to the Action i have defined

jamesw wrote:
  resources :friends do
    collection do
      get "delete_all"
    end
  end

.

it works !
jamesw,thank you so much!

i am a rails rookie