How can I validate from a gem that doesn't have the proper validation calls in the model?
Hello Folks,
I want to do validation for my mail functionality on my web page. I want to require that a recipient has been entered as well as a subject and body when a new mail is written.
The problem is I'm using the Mailboxer gem and it only has validation for the subject; therefore, no error is output for an empty recipient field. In fact, it actually sends without one! Because of this, I am unable to do validation the "typical" way, by checking for "errors" on the object since an empty recipient list is not considered an error.
In the controller, in create, I am able to tell if no recipient has been selected by checking the "recipients" value before it is given to the send_message method to create the "conversation". However, I don't know how to display an error message through the controller when "recipients" is empty. I need to redisplay the form with whatever was entered intact as well as display an error message. flash[:error] doesn't seem to be working, and my render or redirect calls bring up the form with none of the data that was entered.
Here is my code excerpt:
def create
recipients = User.where(id: conversation_params[:recipients])
conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
if (!recipients.any?)
#Here is where I tried various ways to render or redirect and flash an error message for when no recipient has been entered. In all cases, the flash message doesn't show up, and with the redirect or render, I either get a blank screen or the mail form with whatever was entered no longer there.
else #this part works fine
flash[:notice] = "Your message was successfully sent!"
redirect_to conversation_path(conversation)
end
end
Any thoughts would be appreciated. Thank you!