索引操作不显示闪存通知

时间:2013-09-14 16:10:09

标签: ruby ruby-on-rails-3 ruby-on-rails-3.2 routing controllers

我有一个带有destroy动作的trackers_controler。我试图将destroy动作重定向到destroy上的索引,因为所有这些跟踪器都显示在索引的汇总表中。

在我的销毁操作中,当我重定向到tracker_url时,我的flash通知正确显示。但URL具有id(应该如此)

 redirect_to tracker_url, :notice => "Tracker Disabled!"
 redirect with the flash notice displaying
 /trackers/trackers/5

当我使用下面的重定向重定向到索引时,它会在网址中显示闪存通知,但不会在消息中显示闪存通知。

redirect_to :action => index, :notice => "Tracker Disabled!"
redirects without the flash notice
/trackers?notice=Tracker+Disabled%21

要进行调试,我已经复制了index.html.erb和show.html.erb中的代码,以便它们是相同的,并且仍然将flash通知作为url的一部分,甚至认为它们都具有在同一地点。消息的部分位于布局/消息文件夹

<%= render 'layouts/messages' %>

以下是我的节目和索引

def index
   authorize! :view, :silver, :message => 'Access limited to Silver Plan subscribers.'

   @new_tracker = Tracker.new
   @current_user = current_user
   @trackers_enabled = Tracker.enabled.where(:user_id => @current_user.id)
   @trackers_disabled = Tracker.disabled.where(:user_id => @current_user.id)
 end

def show
   authorize! :view, :silver, :message => 'Access limited to Silver Plan subscribers.'

  @new_tracker = Tracker.new
  @current_user = current_user
  @trackers_enabled = Tracker.enabled.where(:user_id => @current_user.id)
  @trackers_disabled = Tracker.disabled.where(:user_id => @current_user.id)
end

从我的开发日志开始,这里是重定向后的帖子

SHOW

重定向到/ trackers / 1 完成302发现于19ms(ActiveRecord:13.8ms)

在2013-09-14 11:06:06 -0500开始获取127.0.0.1的“/ trackers / 1” 由TrackersController处理#show as HTML   参数:{“id”=&gt;“1”}

INDEX

重定向到/ trackers?notice = Tracker + Disabled%21 完成302发现于19ms(ActiveRecord:13.5ms)

开始GET“/ trackers?notice = Tracker + Disabled%21”for 127.0.0.1 at 2013-09-14 11:06:51 -0500 由TrackersController处理#index为HTML   参数:{“notice”=&gt;“Tracker Disabled!”}

1 个答案:

答案 0 :(得分:0)

此行有问题

redirect_to :action => index, :notice => "Tracker Disabled!"

通知部分在您的代码中成为#index的参数,而不是redirect_to的参数。

要修复,请使用命名路径。假设您的索引页面路径为trackers_path,然后是

redirect_to trackers_path, :notice => "Tracker Disabled!"