views:

43

answers:

2

Hello,

I have a Project Index View that shows all the projects in an app

I want that view to show if the user signed in is a member or not....

In the Project Index View I have:

    <% if teammember? %>
        <td>Request to Join</td>
    <% else %>
        <td>Already Joined</td>
    <% end %>

Then in the project's controller I have

def teammember(projectid)
 do some stuff.....
end 

But this gives me a "undefined method `teammember?"

+4  A: 

You don't include the teammember method in the controller, you put that in the helper file (app/helpers/project_helper.rb)

module ProjectHelper
  def team_member?(project_id)
    # include other logic here
    true
  end
end

Then in any view that your Project controller renders, you can do:

<% if team_member?(project.id) %>
  This is a team member.
<% else %>
  This isn't a team member.
<% end %>
Garrett
@Garrett, thank you that makes more sense. What's the rails 3 way of checking to see if the user is a team member or not? defined in the permissions table if a record exists true, if not false. Permissions (proejct_id, user_id). thanks
AnApprentice
+1  A: 

If this is a controller method that you need to access in the view, you can make it available like this:

class ProjectsController < ActionController::Base
  helper_method :team_member?
end

This is essentially the same as if you had defined the method in helpers/projects_helper.rb

Just make sure you call the methods the same: your example shows one with a question mark, and one without.

Andrew Vit