我控制器:Ruby on Rails的閃爍
class GroupsController < ApplicationController
def index
flash[:notice] = "GoodNight"
end
end
和助手:
module FlashesHelper
FLASHES_CLASS ={alert: "danger", notice: "success", warning: "warning"}.freeze
def flash_class(key)
FLASHES_CLASS.fetch(key.to_sym, key)
end
def user_facing_flashes
flash.to_hash.slice(:notice, :alert, :warning)
end
end
我很困惑,當我做:
flash.to_hash.slice(:notice, :alert, :warning)
它不工作,但如果我這樣做:
flash.to_hash.slice("notice", "alert", "warning")
它的工作原理。
如果我嘗試這樣:
book = Hash.new
book[:matz] = "Ruby"
book.to_hash.slice(:matz) # => {:matz => "Ruby"}
book.to_hash.slice("matz") # => {}
請告訴我爲什麼。
你應該多關注一下你的寫作:你不清楚你在問什麼,你的例子看起來可能會更短(你不需要控制器/幫手代碼恕我直言) –
@UlysseBN:no ,我發現控制器/幫手代碼在這裏是必要的,以證明這個問題。這不是一個關於散列的問題,這是關於flash行爲的一個問題。好吧,也許不是[全部]幫手:) –
所以不必要的部分將是最後一個散列 –