HUSTEN
HUSTEN

Reputation: 5197

acts_as_follower doesn't work fine with my Rails3.2 App

Since I enabled remote true, it should change the toggle button "Follow" to "Un-Follow" right away.
But it doesn't. If I reload the page, toggle button shows changed label.
So it means that it's actually changing the value of follow column in background.
I've already spent 3 days for this but I still have no idea:(
Can anyone help me how to solve this? Is there anything wrong with codes? or am I missing some file for JQuery? I'm so newbie to Rails...

Gemfile

...
# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
  gem 'jquery-ui-rails'

end

gem 'jquery-rails'

...

assets/javascripts/application.js

...
//= require twitter/bootstrap
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree 
...

views/shared/_follow_user.html.erb

<% unless user == current_user %>
  <% if current_user.following?(user) %>
    <%= link_to sanitize('<i class="icon-remove icon-white"></i> ') + 'Un-Follow', user_follow_path(user.to_param, current_user.get_follow(user).id), :method => :delete, :remote => true, :class => 'btn' %>             
  <% else %>
    <%= link_to sanitize('<i class="icon-ok icon-white"></i> ') + 'Follow', user_follows_path(user.to_param), :remote => true, :class => 'btn btn-primary' %>             
  <% end %>
<% end %>

views/users/create.js.erb

$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');

views/users/destroy.js.erb

$('#follow_user').html('<%= escape_javascript(render :partial => "shared/follow_user", :locals => {:user => @user}) %>');

controllers/follows_controller.rb

class FollowsController < ApplicationController

  def create
    @user = User.find(params[:user_id])
    current_user.follow(@user)
  end

  def destroy
    @user = User.find(params[:user_id])
    current_user.stop_following(@user)
  end

end

config/routes.rb

...
resources :users, :only => [:index, :show] do
  resources :follows, :only => [:create, :destroy]
end
...

models/user.rb

class User < ActiveRecord::Base

  ...

  acts_as_follower
  acts_as_followable

  ...  

end

views/users/show.html.erb

<h1>Users#show</h1>

<p id="notice"><%= notice %></p>

<p>
  <b>User:</b>
  <%= @user.user_profile.user_id %>
</p>

<p>
  <b>Language:</b>
  <%= @user.user_profile.language_id %>
</p>

<p>
  <b>Country:</b>
  <%= @user.user_profile.country_id %>
</p>

<p>
  <b>Prefecture:</b>
  <%= @user.user_profile.prefecture_id %>
</p>

<p>
  <b>Gender:</b>
  <%= @user.user_profile.gender_id %>
</p>

<p>
  <b>Nickname:</b>
  <%= @user.user_profile.nickname %>
</p>

<p>
  <b>Introduction:</b>
  <%= @user.user_profile.introduction %>
</p>

<p>
  <b>Picture url:</b>
  <%= @user.user_profile.picture_url %>
</p>

<% if user_signed_in? %>
  <div id="follow_user">
    <%= render :partial => "shared/follow_user", :locals => {:user => @user} %>
  </div>
<% end %>

Upvotes: 2

Views: 460

Answers (1)

rbinsztock
rbinsztock

Reputation: 3205

You forget the rendering into your Follows Controller.

here the code for my test i used a bit of your code but i use RubyCas-Server as layer to authentification.

So just check controller, views :)

https://github.com/senayar/follower

Upvotes: 1

Related Questions