3

So, I have 2 follow/unfollow forms in my Rails code. Which are like this

<%= form_tag( {:controller => 'profile', :action => 'unfollow_topic' }) do %>
  <%= hidden_field_tag :topic_id, @topic.id %>
  <button class="question_button" type="submit">Unfollow</button>
<% end %>

and this:

<%= form_tag({:controller => 'profile', :action => 'follow_topic' }) do %>
 <%= hidden_field_tag :topic_id, @topic.id %>
 <button class="question_button" type="submit">Follow</button>
<% end %>

And then I have this javascript code:

<script type="text/javascript">
  $(function(){
    $('#follow form').submit(function(e) {
      $.ajax({
        url: this.action,
        type: 'POST',
        data: $(this).serialize(),
        success: function(response){ $('#follow').html(response); }
      });
    return false;
    });
  });   
</script>

My question is how i can return from my controller the appropriate "follow/unfollow" partial for it to be replaced with the response returned.

What I currently have is this, but it doesn't seems to work:

  respond_to do |format|
    format.html { redirect_to('/' + topic.type + '/' + topic.uri) }
    format.js   { render :partial => 'unfollow_topic' }
  end

But instead if renders the whole page, I believe it's actually responding to the format.html and not the format.js any ideas?

5
  • what is in your '_unfollow_topic.js.erb' file? Have you tried stripping everything out of their and just putting an some alert javascript? alert("Hello!"); That might tell you if you are even getting your partial back. Commented Jul 14, 2011 at 7:00
  • ajax requests are usually used to update part of the page As I've understood from your question you want to update whole page with ajax request Is it correct? Commented Jul 14, 2011 at 7:29
  • also please post a chunk of your log with this request Commented Jul 14, 2011 at 7:30
  • @ipd Yes, I've tried to put an alert on the success stament and I do get a response back. Also, I don't have a _unfllow_topic.js.erb file, should I have it? Wasn't RJS deprecated with rails 3? Commented Jul 14, 2011 at 14:16
  • It will look for that partial, try creating that partial and putting the alert in there and see what you get. Commented Jul 14, 2011 at 23:02

1 Answer 1

5

JavaScript:

  <script type="text/javascript">
      $(function(){
        $('#follow form').submit(function(e) {
          $.ajax({
            url: this.action,
            type: 'POST',
            data: $(this).serialize(),
            dataType: 'html'
            success: function(response){ $('#follow').html(response); }
          });
        return false;
        });
      });   
    </script>

controller:

respond_to do |format|
    format.html  do
      if request.xhr?
        render :partial => 'unfollow_topic'
      else
        redirect_to('/' + topic.type + '/' + topic.uri)
      end
    end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.