2017-02-09 44 views

回答

3

如果你有一個動作的控制,如以下幾點:

def create 
    @activity = Activity.new activity_params 
    if @activity.save 
    flash[:notice] = 'Activity was successfully created!' 
    redirect_to activity_path(@activity) 
    else 
    render :new 
    end 
end 

然後你可以使用flash[:notice]flash哈希的通知設置爲Activity was successfully created!

但是,你是正確的,使用時redirect_to可以傳遞:notice作爲一個選項,因爲這樣的:

def destroy 
    if @activity.destroy 
    redirect_to activities_path, notice: 'Activity was successfully destroy!' 
    else 
    flash.now[:notice] = 'Activity was not destroyed.' 
    end 
end 

如果檢查ActionController::Redirecting module你會看到,通知可以作爲一個選項進行傳遞。

從根本上說,這兩件事情做同樣的事情。他們只是設置不同。

希望這會有所幫助!