Question

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

Was it helpful?

Solution

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

def pundit_user
    current_admin
  end

and it works.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top