ruby on rails - How should a form error page be rendered with Turbolinks 5 -
using new turbolinks 5 in rails application - best way render form error messages. documentation says:
instead of submitting forms normally, submit them xhr. in response xhr submit on server, return javascript performs turbolinks.visit evaluated browser.
so if form submits remote request update should doing js form replace or turbolinks 5 have better way? example -
controller:
def update @success = @team.update_attributes( team_params ) end
update.js
<% if @success %> turbolinks.visit('<%= teams_path %>', {action: 'replace'}); <% else %> $('form').replacewith('<%= j(render partial: '/teams/form') %>'); <% end %>
is there more turbolinks 5 way handle failed update?
i'm still tinkering around well. thing i'd doing differently put conditional logic in controller instead of view.
somethings_controller.rb
def create if @something.save redirect_to @something else render 'errors' end end
errors.js.erb
$('form').replacewith('<%= j(render partial: 'form') %>');
why controller-based redirect work? because in docs turbolinks 5 says...
the turbolinks rails engine performs optimization automatically non-get xhr requests redirect redirect_to helper.
...which take mean they're wrapping our redirect in turbolinks.visit
us. nice thing it'll degrade gracefully users without javascript without having futz respond_to
logic.
Comments
Post a Comment