Forum: Ruby on Rails form_remote_tag comes back before saving really done?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
13d7a3ab12ec57e0d372d7115362cb91?d=identicon&s=25 comopasta Gr (comopasta)
on 2009-02-12 08:11
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.
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-12 08:46
(Received via mailing list)
try a
  current_user.reload
or
  current_user.pending_friends_by_me.reload
in your controller after the save.

maybe this helps.
13d7a3ab12ec57e0d372d7115362cb91?d=identicon&s=25 comopasta Gr (comopasta)
on 2009-02-12 08:52
Ha!

current_user.pending_friends_by_me.reload
Yeah that fixed it!

Thanks MaD!
This topic is locked and can not be replied to.