Rails 4 x Bootstrap 3:如何在控制器视图中生成错误警报
Rails 4 x Bootstrap 3: how to generate an error alert in view from controller
我正在使用 Rails 4 和 Bootstrap 3 构建一个 CRUD 网络应用程序。
我有一个邀请模型,允许用户邀请其他用户加入该应用程序 — 目前正在运行。
这是我的 InvitesController
:
class InvitesController < ApplicationController
def create
# some code
if @invite.save
# some code
redirect_to some_path, notice: 'Invitation was successfully sent.'
else
redirect_to another_path, alert: 'Invitation could not be sent: please make sure you enter a valid email address and choose a role.'
end
end
end
现在,当用户成功发送邀请后,他将被重定向到某个路径并获得 blue notice
准确地告诉他。
但是,当用户没有正确填写邀请表单并且无法成功发送邀请时,他将被重定向到相同的视图(表单所在的视图)并获得 yellow alert
告诉他他的表格有错误。
我想让后者成为 红色 alert
我想 Bootstrap 会允许我使用 redirect_to another_path, error: 'Invitation could not be sent: please make sure you enter a valid email address and choose a role.'
这没有用。
–––––
更新:我在这里寻找的是一种使用 .alert-danger
Bootstrap 警报 class 的方法,我没有只想将警报的颜色从黄色更改为红色。
–––––
Rails / Bootstrap 实现我需要的方法是什么?
这是我完成此操作的方法:
首先我创建了一个 helper method
来将 flash message type
翻译成 bootstrap type
def bootstrap_class_for flash_type
case flash_type.to_sym
when :success
"alert-success"
when :error
"alert-danger"
when :alert
"alert-warning"
when :notice
"alert-info"
else
flash_type.to_s
end
end
然后我创建一个部分来根据其类型呈现存储在 Flash 会话中的消息。
<% flash.each do |type, message| %>
<div class="alert <%= bootstrap_class_for(type) %> fade in">
<button class="close" data-dismiss="alert">×</button>
<%= message.html_safe %>
</div>
<% end %>
如果您使用的是 twitter-bootstrap-rails gem,那么已经有一个很棒的助手可供您使用,它几乎可以处理您已有的一切。
宝石文件
gem 'twitter-bootstrap-rails'
application.html.erb
<%= bootstrap_flash %>
bootstrap_flash
方法在您的视图中使用,也是 flash 显示的地方。我更喜欢将我的放在 application.html.erb 文件中,因为它在我的 大多数 页面中使用。此外,此方法几乎完成了 Moustafa 发布的内容,只是 gem 创建者刚刚为您完成了。
我正在使用 Rails 4 和 Bootstrap 3 构建一个 CRUD 网络应用程序。
我有一个邀请模型,允许用户邀请其他用户加入该应用程序 — 目前正在运行。
这是我的 InvitesController
:
class InvitesController < ApplicationController
def create
# some code
if @invite.save
# some code
redirect_to some_path, notice: 'Invitation was successfully sent.'
else
redirect_to another_path, alert: 'Invitation could not be sent: please make sure you enter a valid email address and choose a role.'
end
end
end
现在,当用户成功发送邀请后,他将被重定向到某个路径并获得 blue notice
准确地告诉他。
但是,当用户没有正确填写邀请表单并且无法成功发送邀请时,他将被重定向到相同的视图(表单所在的视图)并获得 yellow alert
告诉他他的表格有错误。
我想让后者成为 红色 alert
我想 Bootstrap 会允许我使用 redirect_to another_path, error: 'Invitation could not be sent: please make sure you enter a valid email address and choose a role.'
这没有用。
–––––
更新:我在这里寻找的是一种使用 .alert-danger
Bootstrap 警报 class 的方法,我没有只想将警报的颜色从黄色更改为红色。
–––––
Rails / Bootstrap 实现我需要的方法是什么?
这是我完成此操作的方法:
首先我创建了一个 helper method
来将 flash message type
翻译成 bootstrap type
def bootstrap_class_for flash_type
case flash_type.to_sym
when :success
"alert-success"
when :error
"alert-danger"
when :alert
"alert-warning"
when :notice
"alert-info"
else
flash_type.to_s
end
end
然后我创建一个部分来根据其类型呈现存储在 Flash 会话中的消息。
<% flash.each do |type, message| %>
<div class="alert <%= bootstrap_class_for(type) %> fade in">
<button class="close" data-dismiss="alert">×</button>
<%= message.html_safe %>
</div>
<% end %>
如果您使用的是 twitter-bootstrap-rails gem,那么已经有一个很棒的助手可供您使用,它几乎可以处理您已有的一切。
宝石文件
gem 'twitter-bootstrap-rails'
application.html.erb
<%= bootstrap_flash %>
bootstrap_flash
方法在您的视图中使用,也是 flash 显示的地方。我更喜欢将我的放在 application.html.erb 文件中,因为它在我的 大多数 页面中使用。此外,此方法几乎完成了 Moustafa 发布的内容,只是 gem 创建者刚刚为您完成了。