RoR - image with link, error showing the ID in the url-for but not in the image_tag?

238 views Asked by At

I have the following code in a partial:

<%= image_tag(url_for(:controller => "campaigns", :action=>'coupon_preview_text',
:id => campaign_id, :width=>width), {
          :title=>@campaign_name,
          :onclick=>"showBigCouponPreview(" + campaign_id.to_s + ")",  
:alt => @campaign_name }) %>

It's working 100% ok when I have a regular rails action display it, e.g. I get

<img title="Click for Largest image" 
src="/campaigns/coupon_preview_text/1075?width=120" 
onclick="showBigCouponPreview(1075)" 
alt="Click for Largest image">

but when I am using the same partial (that has the image_tag) through an ajax call (for the same record) that used rjs to page.replace_html the partial I am getting

<img title="Click for Largest image" 
src="/campaigns/coupon_preview_text/1075?width=120" 
onclick="showBigCouponPreview(#&lt;Campaign:0x7fe795a32210&gt;)" 
alt="Click for Largest image">

i.e. the record id - 1075 - shows ok in the src attribute but not in the onlick action ??? For some reason that I don't know when the ajax call use rjs to replace that partial it seems to behave differently for the same record.

Clearly there is some sort of issue about getting and showing the records ID as opposed to its internal object ID for the on-click part.

1

There are 1 answers

3
Dylan Markow On BEST ANSWER

Wherever you're setting campaign_id in your ajax call, you must be setting it to your actual Campaign object rather than the object's ID. The reason it works for the :id parameter is because Rails knows to use the object's ID, whereas .to_s doesn't.