Form_remote_tag comes back before saving really done?


#1

Hi,

I am having troubles with a form_remote_tag.
I have this form:

<% form_remote_tag :url => { :action => ‘create’, :controller =>
‘invitations’ } do %>
<%= hidden_field_tag ‘invite’, user.id %>
<%= hidden_field_tag ‘command’, ‘invite_user’ %>
<%= submit_tag(‘Invite’) %>
<% end %>

That ends up if the a controllers function (after entering from
create…):

def invite_user(friend_to_invite)
invited_user = User.find(friend_to_invite)
current_user.request_friendship_with(invited_user)
invitation = Invitation.new
invitation.user_id = current_user.id
invitation.status = 0

if invitation.save

   @pending_friends_by_me = current_user.pending_friends_by_me
   puts "@pending_friends_by_me"
   puts @pending_friends_by_me
   puts "/@pending_friends_by_me"

   render :update do |page|
   page[:sent_invitations].replace_html :partial =>

“sent_invitations”
end

  return
end

end


_sent_invitations is the partial that shows the contents of
@pending_friends_by_me. So you press the button on the form and this
partial is updated with the current invitations sent.

THE PROBLEM

Whe the partial is updated from the function above it still doesn’t
contain the updated @pending_friends_by_me, the last invitation is not
visible. It returns to the view but the contents are still the same. If
I just refresh the page then the partial gets all the invitations.

How to solve this?


I went to the model and used the after_save callback, when that is
called inspecting the invitations then all of them are already present.
But didn’t have access to render functions there, and I don’t think I
should actually call it from ther anyway.

Any hints?

Thanks.


#2

try a
current_user.reload
or
current_user.pending_friends_by_me.reload
in your controller after the save.

maybe this helps.


#3

Ha!

current_user.pending_friends_by_me.reload
Yeah that fixed it!

Thanks MaD!