Rails 案例陈述和闪信
Rails case statement and flash messages
我正在尝试在 rails 5 应用程序中添加 flash msg Bootstrap 3 haml。无论如何,只有 case 语句的 else 部分中的 flash-info 会显示。我的代码有什么问题
module ApplicationHelper
def twitterized_type(type)
case type.to_s
when :errors
"alert-danger"
when :alert
"alert-danger"
when :error
"alert-danger"
when :notice
"alert-success"
when :success
"alert-success"
when :warning
"alert-warning"
else
"alert-info"
end
end
application.html.haml
中的快讯
.container
- flash.each do |type, message|
.alert.alert-dismissable{ :class => twitterized_type(type) }
= message
%button.close{ data: { dismiss: 'alert' } } x
控制器:
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
# Display success flash message then redirect to the saved pin
flash[:success] = 'New pin saved successfully'
redirect_to @pin
else
# Display a fail flash message and render new
flash[:alert] = 'New pin failed to save, please try again'
render 'new'
end
def update
if @pin.update_attributes(pin_params)
# save update and display success flash message
flash[:success] = 'Pin was updated successfully'
redirect_to @pin
else
# flash error message and redirect to edit
flash[:alert] = 'Pin failed to update, please try again'
render 'edit'
end
如何让flash显示出预期效果class
尝试在应用程序助手中使用 type.to_sym 而不是 type.to_s
我正在尝试在 rails 5 应用程序中添加 flash msg Bootstrap 3 haml。无论如何,只有 case 语句的 else 部分中的 flash-info 会显示。我的代码有什么问题
module ApplicationHelper
def twitterized_type(type)
case type.to_s
when :errors
"alert-danger"
when :alert
"alert-danger"
when :error
"alert-danger"
when :notice
"alert-success"
when :success
"alert-success"
when :warning
"alert-warning"
else
"alert-info"
end
end
application.html.haml
中的快讯.container
- flash.each do |type, message|
.alert.alert-dismissable{ :class => twitterized_type(type) }
= message
%button.close{ data: { dismiss: 'alert' } } x
控制器:
def create
@pin = current_user.pins.build(pin_params)
if @pin.save
# Display success flash message then redirect to the saved pin
flash[:success] = 'New pin saved successfully'
redirect_to @pin
else
# Display a fail flash message and render new
flash[:alert] = 'New pin failed to save, please try again'
render 'new'
end
def update
if @pin.update_attributes(pin_params)
# save update and display success flash message
flash[:success] = 'Pin was updated successfully'
redirect_to @pin
else
# flash error message and redirect to edit
flash[:alert] = 'Pin failed to update, please try again'
render 'edit'
end
如何让flash显示出预期效果class
尝试在应用程序助手中使用 type.to_sym 而不是 type.to_s