[英]Rails 4 x Bootstrap 3: how to generate an error alert in view from controller
I am using Rails 4 and Bootstrap 3 to build a CRUD web app. 我正在使用Rails 4和Bootstrap 3来构建CRUD Web应用程序。
I have an Invite model that lets users invite other users to join the app — and this is currently working. 我有一个邀请模型,该模型可让用户邀请其他用户加入该应用程序-目前正在运行。
This is my InvitesController
: 这是我的
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
Now, when a user successfully sends an invite, he is redirected to some path and gets a blue notice
telling him precisely that. 现在,当用户成功发送邀请时,他将被重定向到某个路径,并得到一条蓝色
notice
,准确地告诉他。
However, when a user does not fill the Invite form correctly and the invite cannot be sent successfully, he is redirected to the same view (where the form is) and gets a yellow alert
telling him there was an error in his form. 但是,当用户未正确填写邀请表单且无法成功发送邀请时,他将被重定向到同一视图(表单所在的位置),并显示黄色
alert
告知他表单存在错误。
I would like to make the latter a red alert
and I thought Bootstrap would allow me to do so by using redirect_to another_path, error: 'Invitation could not be sent: please make sure you enter a valid email address and choose a role.'
我想使后者成为红色
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.'
来执行此操作redirect_to another_path, error: 'Invitation could not be sent: please make sure you enter a valid email address and choose a role.'
This did not work. 这没有用。
––––– -----
UPDATE : What I am looking for here is a way to use the .alert-danger
Bootstrap alert class, I don't want to simply change the color of my alert from yellow to red. 更新 :我在这里寻找的是一种使用
.alert-danger
Bootstrap警报类的方法,我不想简单地将警报的颜色从黄色更改为红色。
––––– -----
What is the Rails / Bootstrap way of achieving what I need? 实现我所需的Rails / Bootstrap方法是什么?
Here is the way I accomplish this: 这是我完成此操作的方法:
First I create a helper method
to translate the flash message type
into bootstrap type
首先,我创建一个
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
Then I create a partial to render the messages stored in the flash session according to its type. 然后,我创建了一个部分,以根据其类型呈现在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 %>
If you are using the twitter-bootstrap-rails gem there is a great helper already available for you to use that pretty much handles what you already have in place. 如果您使用的是twitter-bootstrap-rails gem,那么已经可以使用强大的帮助程序来处理已经存在的内容。
gem 'twitter-bootstrap-rails'
<%= bootstrap_flash %>
The bootstrap_flash
method is used inside your views and is where the flash will display. bootstrap_flash
方法用于视图内部,并且将在其中显示闪光灯。 I prefer to put mine inside the application.html.erb file as it is used across most of my pages. 我更喜欢将我的文件放在application.html.erb文件中,因为我的大多数页面都使用了它。 Furthermore, this method pretty much does what Moustafa posted except the gem creator just did it for you.
此外,此方法几乎可以完成穆斯塔法(Moustafa)发布的内容,但宝石创建者只是为您提供了此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.