javascript - Rails 4: Turbolinks redirect with flash -
lets have standard controller create action:
def create @object = object.new(object_params) if @object.save redirect_to @object, flash: {success: "created object successfully."} else render :new end end
now if change redirect use turbolinks:
redirect_to @object, turbolinks: true, flash: {success: "created object successfully."}
the server returns text/javascript response, , browser displaying white page line of text on it:
turbolinks.visit('http:/localhost:3000/objects/1');
how make browser executes code instead of displaying text?
also, how can flash success message appear?
this not direct answer question. suggest using unobtrusive js this. you'd want add remote: true
in form first.
def create @object = object.new(object_params) if @object.save flash.now.success = 'created object successfully.' else flash.now.alert = @object.errors.full_messages.to_sentence end render :js end
create file create.js.erb containing following:
$('#flash').replacewith("<%= j render partial: 'layouts/flash' %>");
- this render :js rendered browser, thereby executing script replace flash container (#flash or whatever id used) new flash messages
- furthermore make sure have partial view file app/layouts/_flash.html.erb contains flash html code.
update:
if not using unobtrusive js. can render partial file following:
def create @object = object.new(object_params) if @object.save flash.now.success = 'created object successfully.' else flash.now.alert = @object.errors.full_messages.to_sentence end render partial: 'layouts/flash' end
then in html, embed following:
$('#myform').ajaxform({ url : '<%= url_for objects_path %>', type: 'post', datatype : 'json', success : function (response) { $('#flash').replacewith(response); } });
p.s. might interested wiselinks (partial view loading; specify target container replaced). because turbolinks
not have support yet partial loading, loads whole body.
Comments
Post a Comment