DeX3
DeX3

Reputation: 5569

Ruby on Rails: Custom actions

I'm new to rails, so I'll just explain my situation to you:

I've got a User Model and a UsersController. Users log in with their email address and a password. Special users can invite other users, by typing the email address of the invitee into a form and hitting submit. The invited user then receives a mail containing a link to activate his account, by entering his password for the first time.

Here's the problem:

The "invitation" form is mapped to the create action of my UsersController atm. But what do I map the "activation" form to?

Is it possible for me to define a custom action "activate" or something that can be accessed like /users/3/activate (of course, there should be some authentication token here too...) and would activate the user with id 3?

I've found some stuff on custom actions, but I don't quite get the hang of it yet.

Thx for any help

Upvotes: 2

Views: 3092

Answers (1)

jdl
jdl

Reputation: 17790

You probably have something like this in your routes file. (If not, please post that.)

resources :users 

If you want to add a custom action that acts on a single User, you can add that as a :member like this.

resources :users do
  member do
    get :activate
  end
end

Note that using a get for something that modifies data is a bit wrong, but you're talking about hitting this from a link in an email.

As you play with routes don't forget that rake routes will show you all of the routes that you currently have available to you.

Upvotes: 8

Related Questions