I'm currently in the process of completing the setup for the
acts_as_follower gem for Rails 4. When I click on the follow button, the
ActionView Exception Missing Template kicks in. Does anyone know how to
create a multi-state Zurb Foundation button for it or even custom radius
button. Simply: "Follow/Unfollow" If you have a better approach, please
share it. These solutions are necessities to development.
def follow
@user = User.friendly.find(params[:id])
if current_user
if current_user == @user
flash[:error] = 'You cannot follow yourself.'
else
current_user.follow(@user)
flash[:notice] = "You are now following #{@user.username}"
end
else
flash[:error] = "You must be logged in to follow
#{@user.username}."
redirect_to :back
end
end
def unfollow
@user = User.friendly.find(params[:id])
if current_user
current_user.stop_following(@user)
flash[:notice] = "You are no longer following #{@user.username}."
else
flash[:error] = "You must be logged in to unfollow
#{@user.username}."
redirect_to user_path(@user)
end
end
def block
@user = User.friendly.find(params[:id])
if current_user
current_user.block(@user)
flash[:notice] = "You have blocked #{@user.username}."
redirect_to user_path(@user)
else
flash[:notice] = "You aren't following #{@user.username}."
end
end
If you created an AJAX solution with the buttons before? Please post the
snippets while explaining your best practice method.
Please post the output of rake routes in your terminal here. It appears as though the gem should be hooking in a route to handle the click, but your Rails app didn't pick it up. Also, did you restart your app after adding the gem and running bundle install?
resources :users, :only => [:show] do
member do
get :follow
get :unfollow
post :follow
post :unfollow
get :block
get :followers
get :following
get :posts
end
end
A message shows you aren't currently following <yourself> constantly.
I'm using the latest version of Acts_As_Follower Gem. Can you help me
structure the solution better. Thanks
The error comes because it's looking for a file `follow.html.erb` in your
app/views/<controller>/ directory. In your `follow` method, you do not
direct it anywhere, or tell it to render any other view, so that's what
it's looking for. Did you want it to go somewhere else?
def follow
@user = User.friendly.find(params[:id])
if current_user
if current_user == @user
flash[:error] = 'You cannot follow yourself.'
else
current_user.follow(@user)
flash[:notice] = "You are now following #{@user.username}"
end
else
flash[:error] = "You must be logged in to follow
#{@user.username}."
redirect_to :back
end
end
The only redirect in that method happens if there is no logged in user. The
rest of it sets the flash, then falls through to the standard render.
By convention, a controller action will render a template with the same
name as the action plus the format extension (html in this case) and
handlers.
You'll need to change this into an AJAX request rather than a link_to. You
could probably use `button_tag` but you'll still need to write the actual
AJAX JavaScript code yourself for it. It's pretty straight-forward in
jQuery, attaching a callback to the click event on the button, and the
callback function does the AJAX request.
In your link_to declaration, add remote: true in the options, and in your controller, add a handler for format.js alongside the format.html in the respond_to block. In that handler's block, tell it what to do. The simplest thing is to render a follow.js.erb, which can contain any JavaScript you like to note to the user that the follow event happened. Change the text of the button to "Following" for example. Add and remove a CSS class to "flash" the button or whatever else makes sense to your users.