0

In trying to set-up policies for my practice app. I'm running into a "No Method Error" in my posts controller.

If we zero in on the post controller and my update method here is the code.

 def update
authorize @post
respond_to do |format|
if @post.update(post_params)

  format.html { redirect_to @post, notice: 'Post was successfully updated.' }
  format.json { head :no_content }
  #redirect_to @post

else
  render :edit
  format.html { render action: 'edit' }
  format.json { render json: @post.errors, status: :unprocessable_entity }
end
end

end

As you can see nothing really special. Just a HTML and JSON rendering of the updated page.

The authorize code points to a helper in pundit that looks up the access policies.

Defined in my Admin.rb model i have.

def editor?
    self.role == 'editor'
  end

The authorize code looks up the policy that corresponds to the method name. It looks in the policy class and starts applying the business rules found here. And here is where the problems start.

I get to def update? @admin.editor? end

And it says undefined method 'editor?' for #<Class:0x007ffb7fa4f6a0>

The code is on the policy branch on Git : https://github.com/wmuengineer/portfolio/tree/policy

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
Adam D. Bell
  • 157
  • 1
  • 1
  • 8

2 Answers2

1

Lesson learned. Read the docs properly. I needed to use

def pundit_user
    current_admin
  end

and it works.

Adam D. Bell
  • 157
  • 1
  • 1
  • 8
0

I am not familiar with gem pundit but looking at code I can suppose that you define pundit_user in a wrong way.

According to documentation you can customize pundit_user as User.find_by_other_means. I think this method should return an instance of User. You redefine pundit_user as Admin. I think you should return instance of class Admin

gotva
  • 5,919
  • 2
  • 25
  • 35
  • I've added the code 'def pundit_user Admin end' and 'def pundit_user Admin.current_admin end' to the top of the ApplicationPolicy and PostPolicy files.. No luck. Am i adding this code in the right place @gotva ? – Adam D. Bell Apr 17 '14 at 06:32
  • from [documentation](https://github.com/elabs/pundit#customize-pundit-user) `Simply define a method in your controller called pundit_user`. I think you should define it in controller – gotva Apr 17 '14 at 06:40
  • Still having the same trouble adding it to the controller. I'm thinking i should just refactor and use the default name. – Adam D. Bell Apr 17 '14 at 07:30