Question

I am using cardmagic/contacts gem to import contacts from email addresses in my application. It is working fine but my problem is I want to catch the authentication error like wrong username/password for following code.

@contacts = Contacts.guess(@username, @password)

I want to display that error to user.

I have tried this but not working for me.

@contacts = Contacts.guess(@username, @password)  rescue Contacts::AuthenticationError  

So please guide me how to do so. Thank you.

Was it helpful?

Solution

def import
  @username = params[:user]
  @password = params[:pass]
  begin
    @contacts = Contacts.guess(@username, @password)  
  rescue
    @error_message = e.message
    render :action => 'new'
  end
  redirect_to root_path
end

new.html.erb:

<% if @error_message %>
  <%= @error_message %>
<% end %>
HTML to get username & password inputs
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top