views:

28

answers:

1

This is pretty simple, I just can't quite get it. I'm sure there's a rails-way to do it.

When creating a new comment, I want the user to be able to enter her name in a field, but have the form submit the matching ID for her name (or toss an error/create new person).

Thanks!

A: 

There are a couple of ways to go about this:

  1. In your controller, split the name on " " and save each to a variable, then do a User.find_by_firstname_and_lastname(firstname, lastname). If none exists, tell the user to create one.
  2. Have the text field that the users enters their name into be an AJAXified field that searches for their username and lets them select the correct one. If none exists, you can then find out immediately and tell them that they need to create an account. If it finds one, it sets the value of a hidden field to the ID of the user that was selected.

Option 1 is very error prone, but option 2 requires quite a bit more work. Honestly, this is going to be a hard one to pull off properly. What do you do if there are two John Smiths? How do they know which one to choose, how do you choose in the backend, etc.

I would highly suggest making it based off of username instead of first name or last name. However, if you show the user which one you are going to use (possibly by showing their username, email, etc) then it could work.

Topher Fangio