views:

79

answers:

3

Hello,

I'm trying to build a helper called

def current_project
    @current_project = Project.find(params[:project_id])
end

I want this so that in the navigation plugin I'm using I can use current_project to populate the navigation.

Problem I'm having is while params[:project_id] is available in the navigation config (navigation.rb) it is not working in the application_helper.rb. I get the following error:

"find_with_ids': Couldn't find Project without an ID"

Here's the navigation plugin, in case you're curious: http://github.com/andi/simple-navigation/

ideas why?

Thanks

+4  A: 

Instead of

def current_project
    @current_project = Project.find(params[:project_id])
end

Try

def current_project( project_id )
        @current_project = Project.find( project_id )
end

When invoking the helper, pass the params[:project_id] for project_id param value.

Jas
+1 for Jas, passing params values to a helper method argument will also make testing helpers infinitely easier.
Tim
But the problem is I'm using a simple-navigation plugin that has a navigation.rb config file which doesn't have access to params[:project_id] Which is why I need the helper to grab it. If I could get the project_id in the navigation.rb config file I wouldn't need the helper. Thoughts?
AnApprentice
Can you store it inside session by the controller handling request which is receiving the params value , e.g. session[:project_id] = params[:project_id] ?
Jas
+6  A: 

If passing in the project_id to the helper it not an option, you can put the method into the controller and mark it as a helper_method. For example:

class ApplicationController < ActionController::Base
  helper_method :current_project

  private
    def current_project
      @current_project ||= Project.find(params[:project_id])
    end
end

Then in your views you can call it with:

<% current_project %>

as you expect.

rjk
+1  A: 

Definitely agree with rjk, you should put it in application controller, because if you don't your controllers can't access the method. The issue you're getting seems to be due to a nil id (e.g. params[:project_id] isn't passed through in every request) presumably it's only passed in the navigation action which switches between projects?

I'm not sure what the internals of simple navigation do, but if params[:project_id] is only passed in the navigation action, then the navigation action should be storing this in the session session[:project_id] = params[:project_id].to_i, then you would define a private controller method + helper as follows, using the session stored id so you can access it in other actions:

class ApplicationController < ActionController::Base
  helper_method :current_project

  private
    def current_project
      @current_project ||= Project.find(session[:project_id])
    end
end
Jeremy
Thanks Jeremy, that's awesome info! Do you happen to know anything about nested resources? I've been pounding my head against the wall all day on this one (http://stackoverflow.com/questions/3910345/form-for-a-nested-resources-permissions-y-model) thanks!
AnApprentice