开发者

Why is link_to adding an id to its output when I don't pass a model id?

I'm using Rails 2.2.2 with the old school routes (i.e. I'm not being RESTful) and I'm noticing strange behavior coming from the Rails link_to method. Here is my call:

link_to("my description", { :controller => controller, :action => action }, :id => "html_id")

I want 开发者_高级运维the method to produce "/controller/action" but instead I'm getting "/controller/action/id". This only happens when link_to is called while processing a request for a "/controller/action/id" URL, and the controller and action are the same as that which I pass to link_to. Example:

  1. I'm on the page at "/controller/action" and the links on that page to "/controller/action" are properly pointing to "/controller/action"
  2. I click a link to "/controller/action/id" and the links on the new page which should point to "/controller/action" now point to "/controller/action/id", where id is the same as the id that was in the previous request.

It seems to me like something is getting confused. There is an id in the request which triggers the link_to call but I don't want it to be referenced and I explicitly don't pass an id parameter. For the record, here are my old school default routes:

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'

Any idea why this is happening and, more importantly, how to stop it? Many thanks.


link_to calls the url_for helper which calls ActionController::url_for If ActionController::url_for is given an :action it assumes the :id of the current page.

I'm not quite sure how to fix it.

Explicitly specifying a nil id, could either work or cause it to break with a nil object error.

link_to "My description", {:controller => controller, :action => action, :id => nil}, :id => "html_id"

You might also be able to get by with a named route to your controler/action pair.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜