开发者

How can I send the results from a form :remote => true in Rails 3 back to the page?

This is part of my controller:

if @invite.save
      U开发者_开发知识库serMailer.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):

if request.xhr?
  render :text => "$('#invite_form').before('<div>Sent!</div>');" , :layout => false
else
  ...
end

(Be sure the form has an id and use that instead of #invite_form.)

Since you're using remote: true on 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜