Flash 未在 Rails 中的同一视图中显示

2023-12-03

成功更新对象后,我需要在同一视图(:编辑)中显示 Flash。如果我重定向到另一个操作 - 一切正常。但是当我需要留在 :edit 时 - 不起作用。有人可以向我解释一下我的错误是什么吗...谢谢!

我的控制器中有以下代码片段:

  def edit
    @setting = Setting.find_by(slug: current_user)
  end

  def update
    @setting = Setting.find_by(slug: current_user)

    if @setting.update(settings_params)
      flash[:success] = I18n.t('admin.settings.edit.success')
    else
      flash[:danger] = I18n.t('admin.settings.edit.not_saved')
    end

    redirect_to edit_admin_setting_url(user: current_user)
  end

routes.rb:

scope ":user/" do
  namespace :admin do
    resource :setting, only: [:edit, :update]
  end
end

And edit.html.erb

      <% if flash[:success].present? %>
        <div class="<%= classes + "alert-success" %>">
          <%= icon("fas", "check") %>

          <%= flash[:success] %>

          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
      <% end %>

我也尝试过:

flash.now[:success] = I18n.t('admin.settings.edit.success')
render :edit

也不行。


在 Rails 7 中turbo期望POST / PUT / PATCH表单提交重定向,通常是create and update控制器动作。

要呈现模板,响应必须具有无效状态,例如:unprocessable_entity,否则 Turbo 在浏览器控制台中显示错误:

turbo.es2017-esm.js:2115 Error: Form responses must redirect to another location
    at k.requestSucceededWithResponse (turbo.es2017-esm.js:679)
    at A.receive (turbo.es2017-esm.js:450)
    at A.perform (turbo.es2017-esm.js:431)

Rails 7 注册表单不显示错误消息

这是设置更新操作的一种方法:

# GET /:user/admin/setting/edit
#
# renders edit.html.erb
def edit
  @setting = Setting.find_by(slug: current_user)
end

# PATCH /:user/admin/setting
#
# redirects to user on success
# renders edit.html.erb on failure
def update
  @setting = Setting.find_by(slug: current_user)

  if @setting.update(settings_params)
                                # when setting updated

    flash[:success] = "Success" # set a message to show on the next request;
                                # we have to redirect to another url

    redirect_to user_url(current_user)
                                # redirect some place else, like user profile
                                # this means we're done with `update` action 
                                # and with current request

  else                          # when update/validation failed
                                # we cannot redirect, because we'll loose our
                                # invalid object and all validation errors
                                # NOTE: sometimes if redirect is required
                                #       errors can be assigned to `flash`

    flash.now[:danger] = "Oops" # set a message to show in this request; 
                                # we have to render a response
                                # NOTE: this might be unnecessary, because form
                                #       will also show validation errors

    render :edit, status: :unprocessable_entity
                                # render edit.html.erb template,
                                # this means we're staying in `update` action 
                                # and in current request
                                # NOTE: this has nothing to do with `edit`
                                #       action at the top
  end
end

您还可以使用 Rails 生成器来获取快速启动代码和一切工作原理的示例。

bin/rails generate scaffold Sample name email
bin/rails db:migrate
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flash 未在 Rails 中的同一视图中显示 的相关文章

随机推荐

  • 为什么使用 if(!$scope.$$phase) $scope.$apply() 是反模式?

    有时我需要使用 scope apply在我的代码中 有时它会抛出 摘要已在进行中 错误 所以我开始寻找解决这个问题的方法并发现了这个问题 AngularJS 调用 scope apply 时防止错误 digest已经在进行中 然而在评论中
  • PyDev 中经典的垂直滚动条在哪里?

    我刚刚更新了 Eclipse 与 PyDev 一起使用 令人惊讶地发现经典的垂直滚动条变成了一些奇怪的东西 如图 屏幕截图 上标记的那样 Eclipse 标准 SDK 版本 开普勒服务版本 2 内部版本号 20140224 0627 我想问
  • 在 Java 中迭代数组的最快方法:循环变量与增强的 for 语句[重复]

    这个问题在这里已经有答案了 在Java中 以老式的方式迭代数组是否更快 for int i 0 i lt a length i f a i 或者使用更简洁的形式 for Foo foo a f foo 对于ArrayList 答案是一样的吗
  • LNK2019:rapidjson 出现“无法解析的外部符号”

    我有一个 Visual C 项目 其中添加了rapidjson 库 经过测试可以正常工作 但是当我添加一个rapidjson Document嵌套类的类型抛出一个LNK2019当我尝试编译时出错 该项目是一个创建DLL的动态库 这是我的定义
  • 在 SQL 中创建临时表

    我正在尝试创建一个临时表 仅选择特定的数据register type 我写了这个查询 但它不起作用 CREATE TABLE temp1 Select egauge dataid egauge register type egauge ti
  • 将列表从 Servlet 传递到 JSP

    当我尝试将 Servlet 中的列表的值设置为会话变量并像 JSP 一样访问它时 doGet HttpSession session request getSession true session setAttribute MySessio
  • 如何查看 Maven 在部署期间发送到服务器的内容?

    我正在尝试使用 Github 的新 Actions CI 服务器将包部署到 Github 的新包功能 进展不顺利 我认为一切都设置正确 但我收到此错误 Failed to execute goal org apache maven plug
  • Unpivot SQL 事物

    我有一些数据 例如 Chocolate Strawberies Oranges 2 3 1 4 2 4 我如何得到回来 Chocolate 2 Chocolate 4 Strawberies 3 Strawberies 2 Oranges
  • 动态 Linq + 实体框架:动态选择的日期时间修改

    我正在尝试找到一种方法 在进行 sql 分组之前将 UTC 时间移至本地 我正在使用 System Linq Dynamic 在这里管理https github com kahanu System Linq Dynamic 它非常适合进行动
  • Galaxy Nexus 上的 Toast 大小

    当我在应用程序中使用 toast 时 尺寸非常非常小 但当另一个应用程序显示吐司时 大小是正常的 如高级任务杀手 或短信 我需要做什么才能得到正常大小的吐司 我有 Galaxy Nexus ICS 4 0 1 我的应用程序使用 SDK AP
  • 获取状态 -1 而不是 401 Angularjs

    我正在尝试从服务器获取响应 该函数看起来是这样的 function getOverview var req method GET url base headers authorization Bearer GottenTokens getS
  • 将 MySQL 数据与易用性解耦

    假设一个简单的酒店预订数据库包含三个表 表 1 预订该表包含入住和退房日期以及一间或多间客房的参考信息和优惠券 如果适用 表 2 房间该表包含所有酒店房间的数据 包括每晚的价格和床位数量 表 3 优惠券该表保存了所有优惠券的数据 选项1 如
  • 如何避免“if”链?

    假设我有这个伪代码 bool conditionA executeStepA if conditionA bool conditionB executeStepB if conditionB bool conditionC executeS
  • Android静默更新apk,然后重新启动应用程序

    好吧 首先我想澄清一下 我并不是想达到任何可疑的目的 我们有自己的企业应用程序 仅适用于我们自己的硬件 我们不使用 Google Play 商店 手机也已root 我已经实现了我们自己的Apk更新机制 到目前为止 我已经使用下面的代码成功地
  • Pandas:使用最后可用的值填充缺失值

    我有一个数据框如下 A B zDate 01 JAN 17 100 200 02 JAN 17 111 203 03 JAN 17 NaN 202 04 JAN 17 109 205 05 JAN 17 101 211 06 JAN 17
  • 解决 Solaris 上未声明的 -llapack 依赖性问题

    我已经发布了一个R封装在CRAN这取决于一些成功编译RcppArmadillo代码 该包构建正确 并且在我尝试过的所有测试系统上没有任何注释 如果有兴趣 CRAN 在这里评论 但是 CRAN 检查失败solaris sparc并且无法加载依
  • NSOutlineView 更改披露图像

    在我的大纲视图中 我正在添加自定义单元格 为了绘制自定义单元格 我正在引用 Cocoa 文档中提供的示例代码 http www martinkahr com 2007 05 04 nscell image and text sample 我
  • 无法链接最小的 Lua 程序

    我有以下简单的 Lua 程序 是从 Programming In Lua 一书中复制的 include
  • 如何在 python 中将现有的 google chrome 配置文件与 selenium chrome webdriver 一起使用?

    我需要加载我完整的现有 google chrome 配置文件以及我登录 google 和网站帐户的所有 chrome 扩展 我正在努力处理这段代码 某处存在语法错误 chrome options Options chrome options
  • Flash 未在 Rails 中的同一视图中显示

    成功更新对象后 我需要在同一视图 编辑 中显示 Flash 如果我重定向到另一个操作 一切正常 但是当我需要留在 edit 时 不起作用 有人可以向我解释一下我的错误是什么吗 谢谢 我的控制器中有以下代码片段 def edit settin