开发者

Field accessing incorrect controller action on onChange

开发者 https://www.devze.com 2022-12-19 11:58 出处:网络
I have a website and before i included the design and some changes(layout, scripts, etc), it was working fine. Then, I don\'t know what happened but the onChange method from my select field is accessi

I have a website and before i included the design and some changes(layout, scripts, etc), it was working fine. Then, I don't know what happened but the onChange method from my select field is accessing "show" action instead of the one that's written in the line.

I have the following at my New view:

<%= 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'}) %>

The idea is that it updates a second select field with records related 开发者_如何学编程to the selected one.

The second select looks like this:

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

Again, it was working great before I introduced some changes, but now it just won't go to "update_subcategories" action, it just goes to "show".

In my routes.rb I've got the following:

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

Does anybody know what's going on?


This is your problem.:

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

Rails will use the first route to appear in routes.rb that matches the parameters given. This behaviour kicks in both when generating urls for output with url_for (which is what the :url option for remote_function does in the background) and mapping urls received by the server to actions.

The above route will generate the route /announcments/update_subcategories so the link will appear right when you view source the source in your browser. But when you go to that link, Rails will match it to the show action of the announcements controller.

The fix depends on which method you're using to define routes for the announcements controller. Regardless of which fix you use, it must come before the bad route. (map.show "/announcements/:permalink", :controller => 'announcements', :action => 'show')

If you're using restful routes the fix is to add a :collection option to the definition.

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

If you're not using restful routes the fix is to add a route.

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

精彩评论

暂无评论...
验证码 换一张
取 消