This is part of my controller:
if @invite.save
UserMailer.invite_email(@invite).deliver
respond_with do |format|
format.html do
if request.xhr?
render :text => "sent" , :layout => false
else
flash[:notice] = "Successfully created invite."
redirect_to @invite
end
end
end
else
render :action => 'new'
end
This is my View:
<h3>Invite a Friend</h3>
<%= form_for @invite, :remote => true do |f| %>
<%= f.email_field :recipient_email, :class => 'submittable' %></br>
<%= f.submit %>
<% end %>
What do I need to do so that, for example, the text “sent” or anything I put there appears right above the form on the View?
I tried :update => :status but that didn’t work.
The short answer is that you have to send back javascript that gets executed instead of just sending back text like “sent”. Using your example (and assuming you’re using jQuery):
(Be sure the form has an
idand use that instead of#invite_form.)Since you’re using
remote: trueon the form it should automatically execute the response. In general though, I think you’d be better served by cleaning up your response logic a little bit and separating the xhr response into its own template. Check out this page under the “Using responds_with” section for some great examples:http://asciicasts.com/episodes/224-controllers-in-rails-3