I have a Projects View that shows a list of all projects... I want to be able to say per project if the currently signed in user is a member or not, and what there role is... In the project View I have: <% if team_member?(project.id, current_user) %> <td>Request to Join</td> <% else %> <td>Already Joined</td> <% end %> then the helper: module ProjectsHelper def team_member?(project_id, current_user) if current_user.permissions.find_by_project_id(project_id).role.try(:name).nil? true else false end end end *** But that errors. Can you help me out? Models: permission.rb class Permission < ActiveRecord::Base belongs_to :user belongs_to :project belongs_to :role end project.rb class Project < ActiveRecord::Base has_many :permissions has_many :users, :through => :permissions end role.rb class Role < ActiveRecord::Base has_many :permissions end user.rb class User < ActiveRecord::Base belongs_to :instance has_many :books has_many :permissions has_many :projects, :through => :permissions -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Error is undefined method `roles'' for nil:NilClass On Sep 17, 10:03 pm, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a Projects View that shows a list of all projects... I want to > be able to say per project if the currently signed in user is a member > or not, and what there role is... > > In the project View I have: > <% if team_member?(project.id, current_user) %> > <td>Request to Join</td> > <% else %> > <td>Already Joined</td> > <% end %> > > then the helper: > > module ProjectsHelper > def team_member?(project_id, current_user) > if > current_user.permissions.find_by_project_id(project_id).role.try(:name).nil ? > true > else > false > end > end > end > > *** But that errors. Can you help me out? > > Models: > permission.rb > > class Permission < ActiveRecord::Base > belongs_to :user > belongs_to :project > belongs_to :role > end > project.rb > > class Project < ActiveRecord::Base > > has_many :permissions > has_many :users, :through => :permissions > > end > role.rb > > class Role < ActiveRecord::Base > has_many :permissions > end > user.rb > > class User < ActiveRecord::Base > > belongs_to :instance > has_many :books > has_many :permissions > has_many :projects, :through => :permissions-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I think the problem is come from this line: current_user.permissions.find_by_project_id(project_id).role.try(:name).nil ? And I am thinking if using try then it doesn''t need to use .nil. You can run this query in console to debug it. Cheers, Lecky Lao On Sep 18, 3:46 pm, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Error is undefined method `roles'' for nil:NilClass > > On Sep 17, 10:03 pm, nobosh <bhellm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I have a Projects View that shows a list of all projects... I want to > > be able to say per project if the currently signed in user is a member > > or not, and what there role is... > > > In the project View I have: > > <% if team_member?(project.id, current_user) %> > > <td>Request to Join</td> > > <% else %> > > <td>Already Joined</td> > > <% end %> > > > then the helper: > > > module ProjectsHelper > > def team_member?(project_id, current_user) > > if > > current_user.permissions.find_by_project_id(project_id).role.try(:name).nil ? > > true > > else > > false > > end > > end > > end > > > *** But that errors. Can you help me out? > > > Models: > > permission.rb > > > class Permission < ActiveRecord::Base > > belongs_to :user > > belongs_to :project > > belongs_to :role > > end > > project.rb > > > class Project < ActiveRecord::Base > > > has_many :permissions > > has_many :users, :through => :permissions > > > end > > role.rb > > > class Role < ActiveRecord::Base > > has_many :permissions > > end > > user.rb > > > class User < ActiveRecord::Base > > > belongs_to :instance > > has_many :books > > has_many :permissions > > has_many :projects, :through => :permissions-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.