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:inmessage_to_seller’
app/models/notification_observer.rb:3:in after_update' /usr/local/lib/ruby/1.9.1/observer.rb:186:inblock 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:innotify_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:inblock in
_send_message_to_seller’
app/controllers/parts_controller.rb:333:in _send_message_to_seller' app/controllers/parts_controller.rb:52:insend_message’

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


def send_message
@part = Part.find(params[:id])

if ( not params[:type].nil? and params[:type].eql?

‘compose_message_to_friend’ )
@page_title = ‘Recommendation for "’ + @part.title + ‘"’

  # composing message to friend
  _send_message_to_friend(@part)
else
  @page_title = 'Enquiry for "' + @part.title + '"'
   puts "the url here is " + part_url(@part)    <-- the value

shows just ok here!

  # composing message to seller
  _send_message_to_seller(@part)
end

end

def _send_message_to_seller(part)
@notification = Notification.new(params[:notification])

@user_message = params[:notification][:message]

params[:notification][:created_by] = 'part enquirer'
params[:notification][:updated_by] = 'part enquirer'

respond_to do |format|
  if @notification.save
    puts "We are inside the url is now " + %{Hi,

“#{part_url(part)}” onto your browser. } <-- the value shows just ok
here!

    @message = %{Hi, "#{part_url(part)}" onto your browser.  }

    @notification.message = @message
    @notification.save  <!-- ERROR thrown here
 end

end

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.

On Tue, Feb 8, 2011 at 11:57 AM, ct9a [email protected] wrote:

hahah

part_url(@part.id)

in my application, I am trying to send out a notification to users.
(eval):17:in part_url' app/controllers/parts_controller.rb:333:in _send_message_to_seller’

params[:notification][:created_by] = ‘part enquirer’
@notification.message = @message


You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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 Y.