首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >闪存不在Rails的相同视图中显示。

闪存不在Rails的相同视图中显示。
EN

Stack Overflow用户
提问于 2022-04-04 07:50:44
回答 1查看 710关注 0票数 2

在成功地更新对象之后,我需要在相同的视图(:编辑)中显示闪存。如果我重定向到另一个动作-一切都正常。但当我需要呆在里面时:编辑-不起作用。谁能给我解释一下我的错误..。谢谢!

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

代码语言:javascript
运行
复制
  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

代码语言:javascript
运行
复制
scope ":user/" do
  namespace :admin do
    resource :setting, only: [:edit, :update]
  end
end

edit.html.erb

代码语言:javascript
运行
复制
      <% 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 %>

我也试过:

代码语言:javascript
运行
复制
flash.now[:success] = I18n.t('admin.settings.edit.success')
render :edit

也不管用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-04 09:58:28

在Rails 7中,turbo希望POST / PUT /修补程序表单提交可以重定向,通常是创建和更新控制器操作。

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

代码语言:javascript
运行
复制
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)

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

代码语言:javascript
运行
复制
# 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生成器获得一个快速启动代码,并举例说明一切如何工作。

代码语言:javascript
运行
复制
bin/rails generate scaffold Sample name email
bin/rails db:migrate
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71733702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档