RESTful route for member action

I have run into this scenario a couple of times and am wondering how
other people get around it. When you create a member route it is
intended to operate on just one of a particular model object. My
problem is that sometimes I want to use a member route on a model
object that may not have been created yet. This throws off the route
generation.

Here is an example:

map.resources :user, :member => { :check_email => :post }

So if I were to create a link with the named route:
user_check_email_path(@user) => /user/1/check_email

But what if the @user is a new object? So I want to perform a member
method on the object before it is created. In that case:
user_check_email_path(@user) => /user//check_email

Shouldn’t Rails generate and recognize the route /user/check_email for
the check_email method against a new unsaved @user object?

Thanks,
Tom

If it’s a new user why do you check their email? Shouldn’t you make
sure @user isn’t nil before you call that action?

user_check_email_path(@user) unless @user.nil?

My real recommendation though is to stick to CRUD actions. If you had
an email_count controller you could use the index method to grab the
count for a user (in addition to making sure it wasn’t nil).

Also, why are you using a POST for this method? POSTs are for creation/
deletion of data not access.

Hope this helps :slight_smile:

ESPNDev

This was a fictional example where an Ajax call (thus the POST) was
being used to verify that an email address has not already been used
in the system so you could show either a new user who is signing up
for the first time or an existing user that is editing their
information. In the new user scenario, their is no existing @user
object…

On Apr 29, 6:31 pm, “[email protected]” <ESPN3.DL-

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs