App design: email a specific user based on conditions
In my application I have one case when I must create a user to save a message.
This case occurs when a visitor creates something by clicking a button for example. Other users who don't click the button can save a m开发者_JS百科essage without their email.
I was thinking to set a session variable so that other parts of the application can adapt.
something like:
session[:require_email] = true
In the controller for the message.
def create
Update
More detail.
A visitor creates a book. Then they add information to the book. To save their changes they must leave an email as they are the creator of the book and manage it. I use this email to create an account so I validate the email.
This new user then invites friends to participate in two ways. A: They fill out a form supplying their emails. B: They send a message via facebook with a link. I already have their emails so they can leave a message and edit it or delete it.
The friends who receive the email link have accounts as I create them as soon as I have their email.
The friends who click the link from facebook do not have accounts. They can visit the book and leave a message but do not have to leave their email. Its up to them. If they leave their email I create a user account and then they can edit their message or delete it etc. If they don't leave their email the cannot edit or delete their message.
So its this third group I have to manage. I was thinking that if they click the link I can set a session variable session[:guest] = true.
Then in the controller I could do the following
unless session[:guest] && params[:user][:email].blank? @user = User.new(params[:user]) end
Is this ok to do?
Should this be done somehow in the model?You should never include any logic like that in the controller. Remember, one of the basic principle of MVC is tiny controller, fat models. This validation sounds, to me, to be needed in the user model.
validates_presence_of :email, :if => :should_validate_email?
def should_validate_email?
# your logic that says if the email should be validated.
end
Hope that helps!
精彩评论