Rails 7中的闪存错误未在application.html.erb中显示。

huangapple go评论70阅读模式
英文:

Rails 7 flash errors not displaying in application.html.erb

问题

I can provide the translation of the code part as requested:

在我的Rails 7应用程序中遇到了警报未显示的问题。

但是,`notices` 是可以显示的。

我在`application.html.erb`中使用`<%= alert %>`来显示警报。

<%= render partial: "shared/navbar" %>
<div class="px-6">
  <div class="mt-6"></div>
  <div>

    <% if notice %>
      <div class="notification is-success is-light text-center">
        <%= notice %>
      </div>
    <% end  %>

    <% if alert %>
      <% debugger  %>
      <div class="notification is-danger is-light text-center">
        <%= alert %>
      </div>
    <% end %>

    <%= yield %>
  </div>
</div>
</body>

我已经在控制器操作中添加了一个“FAILED”记录器,我在Rails日志中看到了它。这证明我们确实进入了“else”条件,并且在无法创建新对象时渲染了“new”页面。但是,我没有看到警报消息。

def create
@rescue = AnimalRescue.new(rescue_params)

respond_to do |format|
  if @rescue.save
    format.html { redirect_to animal_rescue_url(@rescue), notice: "成功创建了救援。" }
    format.json { render :show, status: :created, location: @rescue }
  else
    Rails.logger.info "失败"
    format.html { render :new, status: :unprocessable_entity, alert: "无法创建救援。" }
    format.json { render json: @rescue.errors, status: :unprocessable_entity }
  end
end

Please note that the translation is provided for the code part, and the code itself is unchanged.

英文:

Having an issue in my Rails 7 app of alerts not showing up.

notices, however, are.

I am displaying alerts in application.html.erb using &lt;%= alert %&gt;

&lt;%= render partial: &quot;shared/navbar&quot; %&gt;
&lt;div class=&quot;px-6&quot;&gt;
  &lt;div class=&quot;mt-6&quot;&gt;&lt;/div&gt;
  &lt;div&gt;

    &lt;% if notice %&gt;
      &lt;div class=&quot;notification is-success is-light text-center&quot;&gt;
        &lt;%= notice %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;

    &lt;% if alert %&gt;
      &lt;% debugger %&gt;
      &lt;div class=&quot;notification is-danger is-light text-center&quot;&gt;
        &lt;%= alert %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;

    &lt;%= yield %&gt;
  &lt;/div&gt;
&lt;/div&gt;

</body>

I have added a "FAILED" logger to my controller action, which I am seeing in the Rails logs. This proves to me that we are indeed falling into the "else" condition, and the new page is being rendered upon failure to create a new object. However, I do not see the alert message.

def create
@rescue = AnimalRescue.new(rescue_params)

respond_to do |format|
  if @rescue.save
    format.html { redirect_to animal_rescue_url(@rescue), notice: &quot;Rescue was successfully created.&quot; }
    format.json { render :show, status: :created, location: @rescue }
  else
    Rails.logger.info &quot;FAILED&quot;
    format.html { render :new, status: :unprocessable_entity, alert: &quot;Unable to create rescue.&quot; }
    format.json { render json: @rescue.errors, status: :unprocessable_entity }
  end
end

end

答案1

得分: 1

你过于复杂化了它。要更简单和更灵活,只需使用相同的逻辑来显示各种闪烁消息,唯一需要更改的是单个CSS类:

<% flash.each  do |type, msg| %>
  <%= tag.div msg, 
              class: [
                {
                   notice: 'is-success',
                   alert: 'is-danger'
                   # 等等
                }.fetch(type.to_sym, type.to_s), 
                "notification", 
                "is-light", 
                "text-center"
              ]
  %>
<% end  %>

毕竟,flash只是一个包含字符串的简单散列对象。保持简单。

英文:

You're overcomplicating it. It will be waaay easier and more flexible to just use the same logic to display all kinds of flash messages as the only thing that needs to change is a single CSS class :

&lt;% flash.each do |type, msg| %&gt;
  &lt;%= tag.div msg, 
              class: [
                {
                   notice: &#39;is-success&#39;,
                   alert: &#39;is-danger&#39;
                   # etc
                }.fetch(type.to_sym, type.to_s), 
                &quot;notification&quot;, 
                &quot;is-light&quot;, 
                &quot;text-center&quot;
              ]
  %&gt;
&lt;% end %&gt;

The flash is after all just a simple hash like object that contains strings. KISS.

答案2

得分: 1

format.html { render :new, status: :unprocessable_entity, alert: "无法创建救援。" }
# NOTE:                            不是渲染选项  ^^^^^^

:alert:notice 是用于 redirect_to 方法的选项,它们设置相应的 flash 消息:

flash[:alert] = "无法创建救援。"

另外,flash 只会在重定向后的下一个请求中显示。要在当前请求中显示闪现消息,您需要使用 flash.now

format.html do
  flash.now.alert = "无法创建救援。"
  render :new, status: :unprocessable_entity
end

如果您的表单位于 Turbo 框架内,您需要在框架内显示警报。

英文:
format.html { render :new, status: :unprocessable_entity, alert: &quot;Unable to create rescue.&quot; }
# NOTE:                              not a render option  ^^^^^^

:alert and :notice are options for redirect_to method, which set the corresponding flash message:

flash[:alert] = &quot;Unable to create rescue.&quot;

Also, flash will only display on the next request after the redirect. To display a flash message in the current request you have to use flash.now:

format.html do
  flash.now.alert = &quot;Unable to create rescue.&quot;
  render :new, status: :unprocessable_entity
end

In case your form is inside of the turbo frame, you'll need to display an alert from within the frame.

huangapple
  • 本文由 发表于 2023年3月15日 19:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定