Additional Restful controller action doesn't have @courses available #296

Closed
PHP-Point-Of-Sale opened this Issue Mar 7, 2011 · 5 comments

Comments

Projects
None yet
2 participants
@ghost

ghost commented Mar 7, 2011

As per example (shown below), it would appear if I define another action for my controller CoursesController, it should have available an instance variable @courses. It doesn't have either @Course or @courses. I did this by using logger.debug @courses and logger.debug@course and nothing showed up in the log.

    def some_other_action
  if params[:id]
    @project = Project.find(params[:id])
  else
    @projects = Project.accessible_by(current_ability)
  end
  authorize!(:some_other_action, @project || Project)
end
@ryanb

This comment has been minimized.

Show comment
Hide comment
@ryanb

ryanb Mar 7, 2011

Owner

Do you have load_and_authorize_resource in the controller? That should create the @courses instance variable if params[:id] isn't present.

Update: Also, are you defining any blocks in the Ability class? See if Course.accessible_by(Ability.new(User.first)) works in the console because it can only set @courses if blocks are not used in Ability.

Owner

ryanb commented Mar 7, 2011

Do you have load_and_authorize_resource in the controller? That should create the @courses instance variable if params[:id] isn't present.

Update: Also, are you defining any blocks in the Ability class? See if Course.accessible_by(Ability.new(User.first)) works in the console because it can only set @courses if blocks are not used in Ability.

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Mar 7, 2011

I do have load_and_authorize_resource, here is a snippet of my controller:

class CoursesController < ApplicationController
  load_and_authorize_resource
  before_filter :get_course_categories, :only => [:new, :edit, :create, :update]
  before_filter :get_clients, :only => [:new, :edit, :create, :update]

  def search
    #Temp fix, I shouldn't have to do this
    @courses = Course.accessible_by(current_ability)
    @courses = @courses.where('courses.title LIKE ? and courses.hidden =  ?', "%#{params[:search]}%", false)

    #TODO REFACTOR
    if signed_in? and current_user.is_a?(Patient)
      @courses = @courses.where('id NOT IN 
      (SELECT course_id FROM course_requests WHERE patient_id = ? and accepted IS NULL)', current_user.id)
    end
    @courses = @courses.paginate(:page => params[:page], :per_page => 10)
  end
end

I have no blocks in my ability class.

Course.accessible_by(Ability.new(Patient.first))

returns the courses

ghost commented Mar 7, 2011

I do have load_and_authorize_resource, here is a snippet of my controller:

class CoursesController < ApplicationController
  load_and_authorize_resource
  before_filter :get_course_categories, :only => [:new, :edit, :create, :update]
  before_filter :get_clients, :only => [:new, :edit, :create, :update]

  def search
    #Temp fix, I shouldn't have to do this
    @courses = Course.accessible_by(current_ability)
    @courses = @courses.where('courses.title LIKE ? and courses.hidden =  ?', "%#{params[:search]}%", false)

    #TODO REFACTOR
    if signed_in? and current_user.is_a?(Patient)
      @courses = @courses.where('id NOT IN 
      (SELECT course_id FROM course_requests WHERE patient_id = ? and accepted IS NULL)', current_user.id)
    end
    @courses = @courses.paginate(:page => params[:page], :per_page => 10)
  end
end

I have no blocks in my ability class.

Course.accessible_by(Ability.new(Patient.first))

returns the courses

@ryanb

This comment has been minimized.

Show comment
Hide comment
@ryanb

ryanb Mar 7, 2011

Owner

I think I see the problem now. I'll work on a fix for this. In the meantime you can mark that action as a collection action and it should load it.

load_and_authorize_resource :collection => [:search]
Owner

ryanb commented Mar 7, 2011

I think I see the problem now. I'll work on a fix for this. In the meantime you can mark that action as a collection action and it should load it.

load_and_authorize_resource :collection => [:search]
@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Mar 7, 2011

Thanks!

ghost commented Mar 7, 2011

Thanks!

@ryanb

This comment has been minimized.

Show comment
Hide comment
@ryanb

ryanb Mar 9, 2011

Owner

load collection resources in custom controller actions with no id param - closed by 37102fe

Owner

ryanb commented Mar 9, 2011

load collection resources in custom controller actions with no id param - closed by 37102fe

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment