How to setup the follow/unfollow/block link_to button for Acts_As_Follower Rails 4

172 views
Skip to first unread message

David Williams

unread,
Jul 6, 2015, 3:10:33 PM7/6/15
to rubyonra...@googlegroups.com
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.

https://github.com/tcocca/acts_as_follower

Missing template users/follow, application/follow with {:locale=>[:en],
:formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw,
:ruby, :coffee, :slim, :haml, :jbuilder]}. Searched in:

<%= link_to 'Follow', follow_user_path(@user), class: 'button success
radius small' %>

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

--
Posted via http://www.ruby-forum.com/.

David Williams

unread,
Jul 6, 2015, 3:11:08 PM7/6/15
to rubyonra...@googlegroups.com
If you created an AJAX solution with the buttons before? Please post the
snippets while explaining your best practice method.

Walter Lee Davis

unread,
Jul 7, 2015, 6:02:33 PM7/7/15
to rubyonra...@googlegroups.com
On Jul 6, 2015, at 3:10 PM, David Williams <li...@ruby-forum.com> wrote:

> 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?

Walter

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c2ab8f5e1f0ebaabd4910cde066655b7%40ruby-forum.com.
> For more options, visit https://groups.google.com/d/optout.

David Williams

unread,
Jul 8, 2015, 11:41:40 AM7/8/15
to rubyonra...@googlegroups.com
[GET] /users/:id/follow - follow_user
[GET] /users/:id/unfollow - unfollow_user

JS
_create.js.erb
$('#follow_user').html('<%= escape_javascript(render :partial =>
'follow_user', :locals => {:user => @user}) %>');

//JQuery

_destroy.js.erb
$('#follow_user').html('<%= escape_javascript(render :partial =>
'follow_user', :locals => {:user => @user}) %>');

//JQuery

_follow_user.html.erb

<% unless current_user %>
<% if current_user.following?(user) %>
<%= link_to "Unfollow #{user.username}",
unfollow_user_path(user), :method => :delete, :remote => true %>
<% else %>
<%= link_to "Follow #{user.username}", follow_user_path(user),
class: 'button success radius small', :method => :post, :remote => true
%>
<% end %>
<% end %>


render link
<%= render 'users/follow_user' %>


Routes

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

tamouse pontiki

unread,
Jul 10, 2015, 10:07:37 PM7/10/15
to rubyonra...@googlegroups.com
On Mon, Jul 6, 2015 at 2:09 PM, David Williams <li...@ruby-forum.com> wrote:
Missing template users/follow, application/follow with {:locale=>[:en],
:formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw,
:ruby, :coffee, :slim, :haml, :jbuilder]}. Searched in:

It cannot find a follow.html.erb template view. 

tamouse pontiki

unread,
Jul 10, 2015, 10:12:12 PM7/10/15
to rubyonra...@googlegroups.com
Woops, clicked enter by mistake.

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.

David Williams

unread,
Jul 10, 2015, 11:03:49 PM7/10/15
to rubyonra...@googlegroups.com
I'm in need of the AJAX method that allows a person to follow without
rendering a view.

tamouse pontiki

unread,
Jul 11, 2015, 12:39:56 AM7/11/15
to rubyonra...@googlegroups.com


On Fri, Jul 10, 2015 at 10:03 PM, David Williams <li...@ruby-forum.com> wrote:
I'm in need of the AJAX method that allows a person to follow without
rendering a view.
 
Initially you wanted to use a `link_to` to display the link someone would click on to indicate they wanted to follow someone.

<%= link_to 'Follow', follow_user_path(@user), class: 'button success
radius small' %>

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.

On the server side, if you really wish to render nothing at all, `render nothing: true` will do that. See <http://guides.rubyonrails.org/layouts_and_rendering.html#using-render>.

David Williams

unread,
Jul 11, 2015, 12:57:35 AM7/11/15
to rubyonra...@googlegroups.com
tamouse m. wrote in post #1176466:
> On Fri, Jul 10, 2015 at 10:03 PM, David Williams <li...@ruby-forum.com>
> wrote:
>
>> I'm in need of the AJAX method that allows a person to follow without
>> rendering a view.
>>

>
> On the server side, if you really wish to render nothing at all, `render
> nothing: true` will do that. See <
> http://guides.rubyonrails.org/layouts_and_rendering.html#using-render>.

Thank you, Tamouse.

Walter Lee Davis

unread,
Jul 11, 2015, 9:49:03 AM7/11/15
to rubyonra...@googlegroups.com
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.

Walter
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b8a03e773f44e67af5e2a7acbdaaa0f9%40ruby-forum.com.
Reply all
Reply to author
Forward
0 new messages