Search code examples
javascriptruby-on-railsrubyrubygemsruby-on-rails-5

How can I hide my bootstrap modal form after successfully submitting of form data in rails


I have applied server side validations on my rails form .. using ajax and bootstrap now I want to hide my modal after successfully submitting my form .


Solution

  • I guest that you're using ajax to submit the form in your Bootstrap modal.

    After that, controller action will render the js response (for example: create.js.erb)

    Edit: In order to set conditional statements

    In controller action

    @object = YourModel.new(params[:object])
    unless @object.save
      @errors = @object.errors.full_messages
    end
    

    In create.js.erb

    <% if @errors %>
      /* DO form invalid actions, like show error messages */
    <% else %>
      $('#yourModalID').modal('hide');
    <% end %>