AnApprentice
AnApprentice

Reputation: 111060

Rails 3, How to add an action for Projects

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?"

Upvotes: 2

Views: 257

Answers (2)

Andrew Vit
Andrew Vit

Reputation: 19249

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.

Upvotes: 1

Garrett
Garrett

Reputation: 8090

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 %>

Upvotes: 4

Related Questions