Mike Cuddy Mike Cuddy - 1 year ago 62
Ruby Question

Issue with current user editing/deleting in rails 4

I currently have an issue where I am trying to set the ability that only the user who created a group may edit or delete a group. I am getting the following error: undefined method `user' for #

I have the following code in my group index controller:

<% @nearby_groups.each do |group| %>
<h1><%= group.topic %></h1>
<p><%= group.description %></p>
<% if current_user.id == group.user.id %>
<%= link_to "Edit", edit_group_path(group), class: "ui black button" %>
<%= link_to "Delete", group_path(group), method: :delete, data: { confirm: "Are you Sure?"},
class: "ui inverted red button " %>
<% end %>
<%= link_to "show", group_path(group), class: "ui inverted blue button" %>
<% end %>

On my group show page I have the following code:

<h1>The <%= @group.topic %> Group!</h1>
<p>Description: <em><%= @group.description %></em></p>
<p>Group Address: <em><%= @group.address %></em></p>
<p>Group City: <em><%= @group.city %></em></p>
<p>Group State: <em><%= @group.state %></em></p>
<% if current_user.id == @group.user.id %>
<%= link_to "Edit", edit_group_path(), class: "ui black button" %>
<%= link_to "Delete", group_path(), method: :delete, data: { confirm: "Are you Sure?"},
class: "ui inverted red button " %>
<% end %>

My groups controller looks like this:

def index
@groups = Group.includes(:comments)

def show
@group = Group.find(params[:id])

Finally, my groups model looks like this:

class Group < ActiveRecord::Base
has_many :collections
has_many :comments
has_many :users, :through => :collections

validates :topic, presence: true
validates :description, presence: true, length: { minimum: 10 }
validates :address, presence: true
validates :city, presence: true
validates :address, presence: true

geocoded_by :address
after_validation :geocode

Again, on both my index and show pages for group I get the following error: undefined method `user' for # Thank you for any help.

Answer Source


<% if current_user.id == @group.user.id %>


<% if current_user.id == @group.try(:user).try(:id) %>

Or to account for current_user being nil when user is not logged out:

<% if current_user and current_user.id == @group.try(:user).try(:id) %>


# +try+ calls can be chained:
#   @person.try(:spouse).try(:name)

via: https://github.com/rails/rails/blob/be589a8b01500a4b52a86248458bf57597e568e8/activesupport/lib/active_support/core_ext/object/try.rb#L16

Note: This answer doesn't account for Active Record association issues: http://guides.rubyonrails.org/association_basics.html#the-types-of-associations


According to your has_many_through association, the plural Group#users should return an Active Record relation object which looks like an array containing any associated users.

Change to:

<% if current_user and @group.users.map {|user| user.id}.include?(current_user.id) %>

This type of logic should be moved to the model layer, for example:

# app/models/users.rb
class User
  def has_group_access?(group_id)
    Collection.find_by_group_id_and_user_id(group_id, self.id)

# app/views/whatevs.html.erb
<% if current_user and current_user.has_group_access?(@group.id) %>

Update 2

If group belongs to user, then change to:

<% if current_user and current_user.id == @group.user_id %>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download