【问题标题】:Rails case statement and flash messagesRails 案例语句和闪存消息
【发布时间】:2016-06-18 08:42:40
【问题描述】:

我正在尝试在 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 与预期的类一起显示

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap flash


    【解决方案1】:

    尝试在应用程序助手中使用 type.to_sym 而不是 type.to_s

    【讨论】:

      猜你喜欢
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多