views:

18

answers:

1

After utilizing the great trial and error for over an hour along with dozens of tutorials and blogs posting examples, I still cannot get the simple ajax functionality to work. Here is what is in my partial:

<span id="friend">
  <%= link_to_remote image_submit_tag("/images/add_as_friend.gif"), :url => {:controller => 'friends', :action => 'add', :id => id} %>
</span>

This is what is in my controller:

class FriendsController < ApplicationController
  def add
    unless params[:id].nil?
      Friend.create(:user_id => @current_user.id, :friend_id => params[:id], :friend_type => 2)
    end
  end
end

This is what is in my add.rjs file:

page.replace_html 'friend', "A request to be friends with this player has been sent."

The image for the link comes up fine. When I click on it, however, nothing happens. I've checked the database and nothing is going on. I must be missing something, any thoughts?

A: 

It may be because you are using image_submit_tag rather than image_tag for the content of your link. image_submit_tag is for submitting forms so will have its own onclick behaviour which may override the onclick behaviour that will be added as part of the link_to_remote functionality.

I would try:

<%= link_to_remote image_tag("/images/add_as_friend.gif"), 
    :url => {:controller => 'friends', :action => 'add', :id => id} %>
Shadwell
In addition to this, I did not render anything from the action. I added the line- render :partial => 'friend' and now it's workgin smoothly. Thanks!
scott