ruby on rails - undefined method `has_role?' for nil:NilClass -


In an attempt to implement a rolify method for my user, I get the following error:

  undefined method `has_role? 'For Nil: NilClass  

I do not understand why, because the current user should have a global method accessible to each view? How can this be zero?

Thank you!

Controller

  def show @ photo = photo. Friendly.Fund (Paramos [: ID]) Impressionist (@photos) @photo = Photo.latest_photos @user = @ photo.user @ pin = pin.New @ pin.photo_id = @photo id = category = category.Al @ Commentable = @photo @comments = @ commentable.comments @comment = comment.New @ sponsor = @photo Sponsor @ Zone = Zone.All Respond I | Format | Format.html # show.html.erb format.json {json: @photo} End End  

My View

  & Lt;% if current_user.has_role? : Admin% & gt; & Lt;% = link_to "Eliminar", user_photo_pin_path (user_id: @ user.id, photo_id: @photo.id, id: pin.id), method :: delete, data: {confirm: 'Quieres borrar esto?' }, Category: "Delete Right"% & gt; & Lt;% = link_to 'Editor', edit_user_photo_pin_path (user_id: @ user.id, photo_id: @ photo.id, id: pin.id) ,: class = & gt; "Link", category: "edit quarter_margin_rightright"%> & Lt;% end% & gt; & Lt;% end% & gt;  

You have to check whether any current_user is your Thoughts:

  <% if current_using & amp; Amp; Current_user.has_role? (: Admin)% & gt; & Lt;% = link_to "Eliminar", user_photo_pin_path (user_id: @ user.id, photo_id: @photo.id, id: pin.id), method :: delete, data: {confirm: 'Quieres borrar esto?' }, Category: "Delete Right"% & gt; & Lt;% = link_to 'Editor', edit_user_photo_pin_path (user_id: @ user.id, photo_id: @ photo.id, id: pin.id) ,: class = & gt; "Link", category: "edit quarter_margin_rightright"%> & Lt;% end% & gt; & Lt;% end% & gt;  

This boolean uses short-circuiting - if the current user is zero then current_user.has_role? : Administrator is never evaluated.

If you are using a Divis, you can use the user_signed_in? can also use a helpful method.

added.

If you find yourself often to do this, you can make a helpful method:

  # app / assistants / roles_helper.rb module rollshelper def is_role? (Role) current_user & amp; Amp; ;  

You can simplify your view:

  & lt;% if is_role? (: Admin)% & gt; & Lt;% = link_to "Eliminar", user_photo_pin_path (user_id: @ user.id, photo_id: @photo.id, id: pin.id), method :: delete, data: {confirm: 'Quieres borrar esto?' }, Category: "Delete Right"% & gt; & Lt;% = link_to 'Editor', edit_user_photo_pin_path (user_id: @ user.id, photo_id: @ photo.id, id: pin.id) ,: class = & gt; "Link", category: "edit quarter_margin_rightright"%> & Lt;% end% & gt;  

Note that we have has_role on the visual reference? are calling.


Comments