Search code examples
ruby-on-railsmailchimpgibbon

Add alert after subscribing with gibbon in rails?


How can I create an alert after someone subscribes to an email list in Rails 4 with the Gibbon gem?

<%= form_tag('/static_pages/subscribe', method: "post", :class => 'form-inline', id: "subscribe-form", remote: "true",) do %>
    <div class='input-group'>
   <%= email_field(:email, :address, {id: "email", placeholder: "email address", class: 'btn btn-lg'}) %>
   <%= submit_tag(:submit, class: "btn btn-info btn-lg", id: "email-click", 'data-disable-with' => "Please wait...") %>
   </div>
<% end %>

This is the controller action:

  def subscribe
    @list_id = "43dcea1d12"
    gb = Gibbon::API.new

    gb.lists.subscribe({
      :id => @list_id,
      :email => {:email => params[:email][:address]}
      })

  end

I have tried adding the following before or after the gb.lists.subscribe call without luck

flash[:alert] = "Subscribed!"

Solution

  • I was having the same problem and saw in the server log that the response was in JS. I was also redirecting to the root_path.

    After referencing https://stackoverflow.com/a/18681807 and then https://stackoverflow.com/a/17689223, I solved my problem with:

    respond_to do |format|
        format.html {redirect_to root_path}
        flash[:alert] = "Subscribed!"
        flash.keep(:alert) # Keep flash notice around for the redirect.
        format.js {render :js => "window.location.href='"+root_path+"'"} 
      end
    

    Hope this helps!