Created
October 5, 2018 06:45
-
-
Save rodloboz/1ca63ac74b2be73867d1af42a6f37eea to your computer and use it in GitHub Desktop.
Triggering Sweet Alert with Rails remote: true
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # add remote: true to form | |
| <%= simple_form_for @model, remote: true do |f| %> | |
| # ... | |
| <%= f.button :submit %> | |
| <% end %> | |
| # controller receives ajax request | |
| def create | |
| # do something | |
| if @model.save | |
| # controller responds to ajax requests and html requests | |
| respond_to do |format| | |
| format.html { redirect_to model_path(@model) } | |
| format.js | |
| end | |
| else | |
| respond_to do |format| | |
| format.html { render :new } | |
| format.js | |
| end | |
| end | |
| # controller renders create.js.erb | |
| <% if @model.persisted? %> | |
| swal({ | |
| // do something | |
| }) | |
| <% else %> | |
| swal({ | |
| // do something | |
| }) | |
| <% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment