Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6553 (closed defect: wontfix)

Opened 3 years ago

Last modified 3 years ago

observe_field doesn't honour :with => id with RESTful urls

Reported by: RurouniJones Assigned to: David
Priority: normal Milestone: 1.x
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc:

Description

If you use the standard observe_field code and a select box with a restful controller then you can end up with the following code which should display a single record (Get method with an ID)

<%= observe_field("project_id", :update => "main", :url => project_path, :method => "get", :with => "'id='+escape(value)") %>

However this results in an index request rather than a show request because the project_path() url doesn't have the id specified as a parameter it as it is provided with the :with hash.

Processing ProjectsController#show (for 127.0.0.1 at 2006-11-06 06:44:15) [GET]
  Session ID: 378c759fbbfebd6510edd605de7e1cb5
  Parameters: {"action"=>"show", "id"=>"8", "controller"=>"projects"}

As you cannot provide the project_path url with the id until the select box has been modified you cannot specify that it should display a single record.

So unless I am missing something this means that you cannot use the observe_field helper with RESTful controllers to display, edit or delete a single record.

Change History

11/06/06 07:03:53 changed by RurouniJones

And like an idiot I pasted the wrong server log entry, it should be this:

Processing ProjectsController#index (for 127.0.0.1 at 2006-11-06 06:51:14) [GET]
  Session ID: 378c759fbbfebd6510edd605de7e1cb5
  Parameters: {"action"=>"index", "id"=>"3", "controller"=>"projects", "_"=>nil}

That is the result of using the above code.

01/08/07 23:56:24 changed by manfred

  • status changed from new to closed.
  • resolution set to wontfix.

project_path is nothing more than a named route, and a named route is nothing more than a method. So unless you provide the id directly to the named route project_path(id), it's not going to work. But you can still make it work.

Instead of

<%= observe_field("project_id", :update => "main", :url => project_path, :method => "get", :with => "'id='+escape(value)") %>

Just do something like:

<%= observe_field("project_id", :update => "main", :url => {:controller => 'projects', :action => 'show'}, :method => "get", :with => "'id='+ encodeURIComponent(value)") %>

And a tip, use encodeURIComponent (reference).