I’m trying to modify the following code to make it return the flash notice and success if the email exists.
class SubscribersController < ApplicationController
respond_to :json
def create
@subscriber = Subscriber.new(:first_name => params[:first_name],
:last_name => params[:last_name],
:email => params[:email],
:ip_address => request.remote_ip,
:referring_page => request.referer )
if @subscriber.save
flash[:notice] = "You've been subscribed!"
end
respond_with(@subscriber)
end
end
How could I check if the email address exists already and return success with the flash message?
I don’t want to throw an error if the email already exists (which the model checks for) — I want to still say you’ve been subscribed.
I’ve tried checking with .present? but it throws an error:
<h1>
NoMethodError
in SubscribersController#create
</h1>
<pre>undefined method `model_name' for ActiveRecord::Relation:Class</pre>
I believe, you could use a dynamic finder for this matter.
however, I think, saving a persisted record will return
false, so you need to check if