字段访问onChange上的错误控制器操作

时间:2010-02-02 18:49:21

标签: ruby-on-rails

我有一个网站,在我包含设计和一些更改(布局,脚本等)之前,它工作正常。然后,我不知道发生了什么,但是我的选择字段中的onChange方法正在访问“show”操作而不是在行中写入的操作。

我在新视图中有以下内容:

<%= collection_select('category', 'id', @categories, 'id', 'name', {:prompt => "Choose a category"}, {:onchange => "#{remote_function(:url => {:controller => 'announcements', :action => "update_subcategories"}, :with => "'parent_id='+value")}",:class => 'newAd_box2_inputField'}) %>

这个想法是它更新第二个选择字段,其中包含与所选记录相关的记录。

第二个选择如下:

<%= render :partial => 'category_select', :object => @subcategories %>

同样,在我介绍一些更改之前,它工作得很好,但现在它不会转到“update_subcategories”动作,它只是转到“show”。

在我的routes.rb中,我有以下内容:

  map.show "/announcements/:permalink", :controller => 'announcements', :action => 'show'
  map.new "/announcements/new", :controller => 'announcements', :action => 'new'

有人知道发生了什么吗?

1 个答案:

答案 0 :(得分:1)

这是你的问题。:

map.show "/announcements/:permalink", :controller => 'announcements', 
  :action => 'show'

Rails将使用第一个路由出现在routes.rb中,该路由与给定的参数匹配。当使用url_for生成用于输出的URL时(这是remote_function在后台执行的url选项)和将服务器接收的URL映射到操作时,此行为都会发生。

上述路线将生成路线/announcments/update_subcategories,因此当您在浏览器中查看来源时,链接将会正确显示。但是当你转到那个链接时,Rails会将它与公告控制器的show动作相匹配。

修复取决于您使用哪种方法来定义通知控制器的路由。无论你使用哪种修复方法,它都必须在不良路线之前。 (map.show "/announcements/:permalink", :controller => 'announcements', :action => 'show'

如果您使用的是restful路由,则修复方法是在定义中添加:collection选项。

map.resources :announcements, :collection => {:update_subcategories => :get}

如果您没有使用restful路线,则修复方法是添加路线。

map.connect "/announcements/update_subcategories", 
  :controller => "announcements", :action => "update_subcategories"