views:

26

answers:

1

I'm experiencing problems with the :with clause in the following code.

-counter.times do |c|
  -elem = @tags[c]
  #{elem.name}#{link_to_remote image_tag('x.png'), :url => {:controller => 'questions', :action => 'remove_tag_from_cart'}, :with => "'tag_remove=' + #{elem}"}

If I take out the :with clause, then remove_tag_from_cart gets executed when I click on the image link. But, I can't do anything with elem because it's not being passed to remove_tag_from_cart in any way.

If I leave the :with clause in there, then remove_tag_from_cart does not get executed at all!

From my understanding, :with is supposed to enable me to do this in remove_tag_from_cart:

def remove_tag_from_cart
  puts params[:tag_remove].name
end

Again, if I include the :with clause, this method does not run.

What I'd like is to just pass elem or elem.id to remove_tag_from_cart so that I can use it (to find things, etc.). Is this (i.e., passing it using :with) the best/right way to do that? If not, what are my alternatives?

+1  A: 

Use firebug to trace ajax calls, view generated html etc.

:with option is (from docs):

A JavaScript expression specifying the parameters for the XMLHttpRequest. Any expressions should return a valid URL query string.

Example:

:with => "'name=' + $('name').value"

You are doing :with => "'tag_remove=' + #{elem}"

elem seems to be ActiveRecord object, so it seems that what you really want to do, is to pass it's id with the params. But you can do it with the normal url params, without using javascript's :with:

link_to_remote image_tag('x.png'), :url => {:controller => 'questions', :action => 'remove_tag_from_cart', :tag_remove_id => elem.id}

and in the conroller use

Tag.find(params[:tag_remove_id])
Voyta