【发布时间】:2019-09-14 11:39:36
【问题描述】:
我有启用 Turbolinks 的 <%= simple_form_for @offer, remote: true %>。我正在尝试在我的update 操作中重定向并显示成功闪光:
respond_to do |format|
if @offer.update(offer_params)
flash[:success] = "#{@offer.name} #{t(:updated)}"
format.html { redirect_to seller_offers_path }
format.js
else
format.html { render :edit }
end
end
我的update.js.erb 的代码与重定向或闪存无关:
$(".ibox-content.actions").removeClass('sk-loading');
$('.sk-spinner').css("display", "none");
在 application.html.erb 中我可以用这个来渲染 flash:
<div class="row">
<% flash.each do |message_type, message| %>
<%= content_tag(:div, content_tag(:strong, message), id: "flash_message",
class: "text-bold alert alert-#{message_type}") %>
<% end %>
</div>
目前我的表单已保存,但没有重定向。
当我在update.js.erb 表单中添加Turbolinks.visit(<%= seller_offers_path %>) 保存时,重定向完成,但是没有flash。
请问有什么方法可以让我使用 Turbolinks 和 remote: true 在重定向页面上显示 Flash 吗? 到目前为止,我已经尝试过 this suggestion,但它仅适用于在同一页面上显示 Flash页面 - 它不适用于 redirect_to。
更新
这确实重定向,但没有闪光:
respond_to do |format|
if @offer.update(offer_params)
format.js { redirect_to seller_offers_path, success: "#{@offer.name} #{t(:updated)}" }
else
format.html { render :edit }
end
end
更新 2
目前我正在尝试实现类似的东西
Turbolinks.visit(url, { keep: 'flash' }); 与 mentioned in docs 一样,但似乎不适用于 Turbolinks 5.2
更新 3
我已经删除 update.js.erb 并且在我的控制器中我有
def update
if @offer.update(offer_params)
flash[:success] = "#{@offer.name} #{t(:updated)}"
redirect_to seller_offers_path
else
render :edit
end
end
重定向已按预期完成,但index 上仍没有显示 Flash。我可以在update 操作结束时打印我的闪存:
#<ActionDispatch::Flash::FlashHash:0x00007f180d2a1c60 @discard=#<Set: {}>, @flashes={"success"=>"My offer updated!"}, @now=nil>
但是在index 操作结束时(我重定向到的地方)它是空的:
#<ActionDispatch::Flash::FlashHash:0x00007f180eb3b780 @discard=#<Set: {}>, @flashes={}, @now=nil>
看起来我的 flash 没有留给下一个请求。是因为 Turbolinks,还是其他什么原因?任何提示我都会很高兴。
【问题讨论】:
-
如果要重定向,为什么需要用remote: true 提交表单?
-
@Tashows 我不希望我的页面完全刷新 - 例如,Turbolinks 可以用
index操作中的内容替换我的视图。 -
嘿@matiss,也许这会对你有帮助stackoverflow.com/questions/23967390/…
-
@Violeta 谢谢,我已经为没有重定向的 AJAX 提供了解决方案,但是我正在寻找
Turbolinks.visit的解决方案 - 请参阅上面的更新 2。
标签: ruby-on-rails simple-form turbolinks ujs ruby-on-rails-6