On Thursday, 21 January 2016 22:21:37 UTC-6, Ruby-Forum.com User wrote:
>
> Matt Jones wrote in post #1180578: 
> > On Tuesday, 12 January 2016 00:12:04 UTC-5, Ruby-Forum.com User wrote: 
> >> follow to unfollow. 
> >> 
>
> I'm returning for your advice and help. I've implemented a 
> Follows_Controller.Rb Below 
>
>
> class FollowsController < ApplicationController 
>   def create 
>     @user = User.find(params[:user_id]) 
>     current_user.follow(@user) unless current_user.blocked?(@user) 
>   end 
>
>   def destroy 
>     @user = User.find(params[:user_id]) 
>     current_user.stop_following(@user) 
>   end 
> end 
>
>
> The JavaScript create and destroy files for the AJAX buttons. 
>
> create.js.erb 
> $('#follow_user').html('<%= escape_javascript(render :partial => 
> "follows/follow_user", :locals => {:user => @user}) %>'); 
>
> destroy.js.erb 
> $('#follow_user').html('<%= escape_javascript(render :partial => 
> "follows/follow_user", :locals => {:user => @user}) %>'); 
>
> The follow user partial file. 
> <% if user_signed_in? %> 
> <% unless @user == current_user %> 
>     <% if current_user.following?(@user) %> 
>         <%= button_to('Stop Following', user_follow_path(user, 
> current_user.get_follow(@user).id), :method => :delete, :remote => true, 
> class: 'btn btn-danger-outline') %> 
>     <% else %> 
>         <%= button_to('Follow', user_follows_path(@user), :remote => 
> true, class: 'btn btn-success-outline') %> 
>     <% end %> 
> <% end %> 
> <% end %> 
>
>
> Lastly, the routes. 
> resources :follows, :only => [:create, :destroy] 
>
>
> When I render the partial inside of the _posts.html.erb file, it gives 
> me an error. First it was 'user' method cannot be found, and then I used 
> the @user instead.


The behavior you've described doesn't match the code. The `:locals => { 
:user => @user }` part of those render calls should have made `user` 
available in the partial. If you're switching to using the instance 
variable, make sure to remove all the references to the local variable 
(there's still one in the call to `user_follow_path`).


Afterwards it started to say undefined method id for 
> nil:NilClass. Why am I unable to use the follow/unfollow buttons? 
>

That message isn't particularly helpful without a stack trace; something is 
calling `id` on an object that's nil. A stack trace would be helpful for 
finding the cause.

Regarding your followup message ("I want the buttons to show, but only 
redirect users that are not logged in."), that may require some rethinking 
of your approach. Doing a redirect from the `FollowsController#create` 
method will not do what you want (the browser will follow the redirect and 
attempt to parse the result as Javascript).

--Matt Jones

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/8a352428-2150-4646-96c2-ffffa00aaaf1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to