1

I am very new and quite scared of jquery. I'll get over it soon but what I am trying achieve is that, after a form has been submitted an alert pops up to say it was a success or failiure. I have this working, but it is the ugly alert popup. what I would like is the bootsrap alert notice or alert warning.

My controller is currently looking like this

  def create
    @subscription = Subscription.new(subscription_params)

    respond_to do |format|
      if @subscription.save
        format.html 

        format.js { render :js=>'alert("You have been added to the mailing list");' }
      else
        format.html 
        format.js { render :js=>'alert("You have entered an invalid email address");' }
      end
    end
  end

I have bootstrap.js loaded I am just unsure how to implement it.

1
  • 1
    You should open modal using jquery $('#myModal').modal(options); refer bootstrap documentation Commented Jul 5, 2014 at 16:13

1 Answer 1

1

Create your modal, assuming it has an id "myModalSuccess". You don't need a modal in case of a failure, you just need to render your form and it will display errors for you. Your controller will look like:

def create
  @subscription = Subscription.new(subscription_params)

  respond_to do |format|
    if @subscription.save
      format.html{redirect_to your_path}
      format.js{}
    else
      format.html{render action: 'new'} 
    end
  end
end

Now you can call it inside your create.js.erb file like:

  $("#myModalSuccess").modal("show");

You can also pass other options inside modal method, for details look into bootstraps javascript documentation

Sign up to request clarification or add additional context in comments.

5 Comments

what if the validation fails, how do I render the errors?
@rico_mac for that i think you should simply render your new action. I don't think you need your modal in case of a failure as your form will show up errors anyways. Correcting my answer as well
yeah i know what you mean. It's just I have a form like this, so i dont think I'd be able to <%= bootstrap_form_for Subscription.new, layout: :inline, url: ' /subscriptions', html: { remote: true } do |f| %> <%= f.text_field :email, hide_label: :true %> <%= f.submit 'Join Mailing List' %> <% end %>
Thanks for your answer though dude. helped.
@rico_mac you can still render your new action and it'll display validation errors in your form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.