url generator doesn't work

hi all,

in my application, I am trying to send out a notification to users.

I can call part_url(@part) from the calling method in the controller BUT when I try to put the value of the parts_url into a notification object, it fails. Here's the message: ActionController::RoutingError (part_url failed to generate from {:controller=>"parts", :action=>"show"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["parts", :id] - are they all satisfied?):   (eval):17:in `part_url'   app/models/notification_mailer.rb:11:in `message_to_seller'   app/models/notification_observer.rb:3:in `after_update'   /usr/local/lib/ruby/1.9.1/observer.rb:186:in `block in notify_observers'   /usr/local/lib/ruby/1.9.1/observer.rb:185:in `each'   /usr/local/lib/ruby/1.9.1/observer.rb:185:in `notify_observers'   /usr/local/lib/ruby/gems/1.9.1/gems/after_commit-1.0.8/lib/ after_commit/connection_adapters.rb:12:in `transaction_with_callback'   app/controllers/parts_controller.rb:381:in `block in _send_message_to_seller'   app/controllers/parts_controller.rb:333:in `_send_message_to_seller'   app/controllers/parts_controller.rb:52:in `send_message'

Here's what the method looks like in myApp/app/controllers/ parts_controller.erb.

maybe you can try this:

part_url(@part.id)

hi, all,

I figured out why. I was updating my source codes and i actually put another call for parts_url in my notification_mailer.rb under the model subdirectory.

Lesson of the day - never call *_url within your model especially when the object does not belong to the class where the model is being called.

hahah

@hendra - thanks for your reply but i doubt passing @part.id would work as part_url was expecting an object...

Congratulations on solving your problem. However, I just want to clarify one thing regarding named routes.

(Just to help users reading the thread)

What Hendra said isn’t wrong. You can pass anything to part_url and it will be passed to the controller as

params[:id]. To make our codes easier to read, the core developers allowed us to pass an object to part_url.

Someone correct me if I’m wrong, but I believe passing an object to part_url uses the object’s to_param method which

returns the id by default. So part_url(@part) and part_url(@part.id) should return the same string if you didn’t

override @part’s to_param method.

Cool :slight_smile: Good to hear.

In perl, we used to pass the id of objects we want to methods (ie. search) and have a result given back. I know these days, we pass objects instead of primary ids which is cool :slight_smile:

Cheers:D

Gordon Yeong