Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get Ajax working in Rails?
    primarykey
    data
    text
    <p>I'm working through the Ruby on Rails tutorial by Michael Hartl (http://ruby.railstutorial.org/ruby-on-rails-tutorial-book) and I'm getting stuck at the point where I'm trying to get AJAX functionality to work.</p> <p>I have a 'follow / unfollow' button on a user profile page that I want to dynamically update when it is clicked. The code I've tried implementing is as follows:</p> <p>*<strong>_follow.html.erb:*</strong></p> <pre><code>&lt;%= form_for current_user.relationships. build(:followed_id =&gt; @user.id), :remote =&gt; true do |f| %&gt; &lt;div&gt;&lt;%= f.hidden_field :followed_id %&gt;&lt;/div&gt; &lt;div class="actions"&gt;&lt;%= f.submit "Follow" %&gt;&lt;/div&gt; &lt;% end %&gt; </code></pre> <p>*<strong>_unfollow.html.erb*</strong></p> <pre><code>&lt;%= form_for current_user.relationships.find_by_followed_id(@user), :html =&gt; { :method =&gt; :delete }, :remote =&gt; true do |f| %&gt; &lt;div class="actions"&gt;&lt;%= f.submit "Unfollow" %&gt;&lt;/div&gt; &lt;% end %&gt; </code></pre> <p>As you can see, I've used the ':remote => true' call to tell the controller to go with the js implementation of the page (see following code):</p> <pre><code>class RelationshipsController &lt; ApplicationController before_filter :authenticate def create @user = User.find(params[:relationship][:followed_id]) current_user.follow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end def destroy @user = Relationship.find(params[:id]).followed current_user.unfollow!(@user) respond_to do |format| format.html { redirect_to @user } format.js end end end </code></pre> <p>For some reason, the actual javascript never updates on the page - when I look in the logs it even says that it has rendered the new element... but it won't change the view of the page.</p> <p>Any thoughts on what's happening here? Do I need to do something else in a configuration file to get javascript / Ajax to work for this application?</p> <p>Thanks in advance for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload