Authorization on my Personal Blog problem
I'm having some trouble writing policies using Pundit on a personal project of mine.
For some reason, I get an Authorization Not Performed Error whenever I try to update a blog post and I can't create a blog post without submitting an empty post.
Here's my Posts Controller:
class PostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
after_action :verify_authorized, except: [:index, :show]
def index
@posts = Post.order(created_at: :desc).page(params[:page]).per(10)
#authorize @posts
end
def new
authorize @post
@post = current_user.posts.build
if @post.save
redirect_to @post, notice: 'Post was successfully created.'
else
render :new
end
end
def show
set_post
end
def edit
set_post
authorize @post
end
def update
set_post
authorize @post
@post.update(post_params)
redirect_to @post
end
def destroy
set_post
authorize @post
@post.destroy
redirect_to action: "index", notice: "The post was removed"
end
def upvote
@post.upvote_from current_user
authorize @post
end
def downvote
@post.downvote_from current_user
authorize @post
end
def create
@post = current_user.posts.build(post_params)
authorize @post
respond_to do |format|
if @post.save
format.html {redirect_to @post, notice: 'Blog post has been posted!'}
format.json {render :show, status: :created, location: @post}
else
format.html {render :new}
format.json {render json: @post.errors, status: :unprocessable_entity}
end
end
end
private
def post_params
params.require(:post).permit(:title, :content, :header_image, uploads: )
end
def set_post
@post = Post.friendly.find(params[:id])
# authorize @post
end
end
Here's my Post Policy as well. I don't know if the error is coming from the policy or the Controller:
class PostPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.where(user_id: @user.try(:id))
end
end
attr_reader :user, :post
def initialize(user, post)
@user = user
@post = post
end
def show?
true
end
def index?
true
end
def create?
is_contributor_or_admin?
end
def update?
is_author_of_post_or_admin?
end
def destroy?
is_author_of_post_or_admin?
end
private
def user_not_authorized
flash[:alert] = "Can't let you do that, " + @user.username + "!"
end
def is_admin?
@user.role_id == 1
end
def is_contributor_or_admin?
@user.role_id == 1 || @user.role_id == 2
end
def is_author_of_post_or_admin?
@user.role_id == 1 || @post.user.username == @user.username
end
end
I hope that I can find a way to create policies. If there is any more information that you may need like code for other files, or any issues regarding the formatting, please let me know.
ruby-on-rails pundit ruby-on-rails-5.2
add a comment |
I'm having some trouble writing policies using Pundit on a personal project of mine.
For some reason, I get an Authorization Not Performed Error whenever I try to update a blog post and I can't create a blog post without submitting an empty post.
Here's my Posts Controller:
class PostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
after_action :verify_authorized, except: [:index, :show]
def index
@posts = Post.order(created_at: :desc).page(params[:page]).per(10)
#authorize @posts
end
def new
authorize @post
@post = current_user.posts.build
if @post.save
redirect_to @post, notice: 'Post was successfully created.'
else
render :new
end
end
def show
set_post
end
def edit
set_post
authorize @post
end
def update
set_post
authorize @post
@post.update(post_params)
redirect_to @post
end
def destroy
set_post
authorize @post
@post.destroy
redirect_to action: "index", notice: "The post was removed"
end
def upvote
@post.upvote_from current_user
authorize @post
end
def downvote
@post.downvote_from current_user
authorize @post
end
def create
@post = current_user.posts.build(post_params)
authorize @post
respond_to do |format|
if @post.save
format.html {redirect_to @post, notice: 'Blog post has been posted!'}
format.json {render :show, status: :created, location: @post}
else
format.html {render :new}
format.json {render json: @post.errors, status: :unprocessable_entity}
end
end
end
private
def post_params
params.require(:post).permit(:title, :content, :header_image, uploads: )
end
def set_post
@post = Post.friendly.find(params[:id])
# authorize @post
end
end
Here's my Post Policy as well. I don't know if the error is coming from the policy or the Controller:
class PostPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.where(user_id: @user.try(:id))
end
end
attr_reader :user, :post
def initialize(user, post)
@user = user
@post = post
end
def show?
true
end
def index?
true
end
def create?
is_contributor_or_admin?
end
def update?
is_author_of_post_or_admin?
end
def destroy?
is_author_of_post_or_admin?
end
private
def user_not_authorized
flash[:alert] = "Can't let you do that, " + @user.username + "!"
end
def is_admin?
@user.role_id == 1
end
def is_contributor_or_admin?
@user.role_id == 1 || @user.role_id == 2
end
def is_author_of_post_or_admin?
@user.role_id == 1 || @post.user.username == @user.username
end
end
I hope that I can find a way to create policies. If there is any more information that you may need like code for other files, or any issues regarding the formatting, please let me know.
ruby-on-rails pundit ruby-on-rails-5.2
add a comment |
I'm having some trouble writing policies using Pundit on a personal project of mine.
For some reason, I get an Authorization Not Performed Error whenever I try to update a blog post and I can't create a blog post without submitting an empty post.
Here's my Posts Controller:
class PostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
after_action :verify_authorized, except: [:index, :show]
def index
@posts = Post.order(created_at: :desc).page(params[:page]).per(10)
#authorize @posts
end
def new
authorize @post
@post = current_user.posts.build
if @post.save
redirect_to @post, notice: 'Post was successfully created.'
else
render :new
end
end
def show
set_post
end
def edit
set_post
authorize @post
end
def update
set_post
authorize @post
@post.update(post_params)
redirect_to @post
end
def destroy
set_post
authorize @post
@post.destroy
redirect_to action: "index", notice: "The post was removed"
end
def upvote
@post.upvote_from current_user
authorize @post
end
def downvote
@post.downvote_from current_user
authorize @post
end
def create
@post = current_user.posts.build(post_params)
authorize @post
respond_to do |format|
if @post.save
format.html {redirect_to @post, notice: 'Blog post has been posted!'}
format.json {render :show, status: :created, location: @post}
else
format.html {render :new}
format.json {render json: @post.errors, status: :unprocessable_entity}
end
end
end
private
def post_params
params.require(:post).permit(:title, :content, :header_image, uploads: )
end
def set_post
@post = Post.friendly.find(params[:id])
# authorize @post
end
end
Here's my Post Policy as well. I don't know if the error is coming from the policy or the Controller:
class PostPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.where(user_id: @user.try(:id))
end
end
attr_reader :user, :post
def initialize(user, post)
@user = user
@post = post
end
def show?
true
end
def index?
true
end
def create?
is_contributor_or_admin?
end
def update?
is_author_of_post_or_admin?
end
def destroy?
is_author_of_post_or_admin?
end
private
def user_not_authorized
flash[:alert] = "Can't let you do that, " + @user.username + "!"
end
def is_admin?
@user.role_id == 1
end
def is_contributor_or_admin?
@user.role_id == 1 || @user.role_id == 2
end
def is_author_of_post_or_admin?
@user.role_id == 1 || @post.user.username == @user.username
end
end
I hope that I can find a way to create policies. If there is any more information that you may need like code for other files, or any issues regarding the formatting, please let me know.
ruby-on-rails pundit ruby-on-rails-5.2
I'm having some trouble writing policies using Pundit on a personal project of mine.
For some reason, I get an Authorization Not Performed Error whenever I try to update a blog post and I can't create a blog post without submitting an empty post.
Here's my Posts Controller:
class PostsController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
after_action :verify_authorized, except: [:index, :show]
def index
@posts = Post.order(created_at: :desc).page(params[:page]).per(10)
#authorize @posts
end
def new
authorize @post
@post = current_user.posts.build
if @post.save
redirect_to @post, notice: 'Post was successfully created.'
else
render :new
end
end
def show
set_post
end
def edit
set_post
authorize @post
end
def update
set_post
authorize @post
@post.update(post_params)
redirect_to @post
end
def destroy
set_post
authorize @post
@post.destroy
redirect_to action: "index", notice: "The post was removed"
end
def upvote
@post.upvote_from current_user
authorize @post
end
def downvote
@post.downvote_from current_user
authorize @post
end
def create
@post = current_user.posts.build(post_params)
authorize @post
respond_to do |format|
if @post.save
format.html {redirect_to @post, notice: 'Blog post has been posted!'}
format.json {render :show, status: :created, location: @post}
else
format.html {render :new}
format.json {render json: @post.errors, status: :unprocessable_entity}
end
end
end
private
def post_params
params.require(:post).permit(:title, :content, :header_image, uploads: )
end
def set_post
@post = Post.friendly.find(params[:id])
# authorize @post
end
end
Here's my Post Policy as well. I don't know if the error is coming from the policy or the Controller:
class PostPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.where(user_id: @user.try(:id))
end
end
attr_reader :user, :post
def initialize(user, post)
@user = user
@post = post
end
def show?
true
end
def index?
true
end
def create?
is_contributor_or_admin?
end
def update?
is_author_of_post_or_admin?
end
def destroy?
is_author_of_post_or_admin?
end
private
def user_not_authorized
flash[:alert] = "Can't let you do that, " + @user.username + "!"
end
def is_admin?
@user.role_id == 1
end
def is_contributor_or_admin?
@user.role_id == 1 || @user.role_id == 2
end
def is_author_of_post_or_admin?
@user.role_id == 1 || @post.user.username == @user.username
end
end
I hope that I can find a way to create policies. If there is any more information that you may need like code for other files, or any issues regarding the formatting, please let me know.
ruby-on-rails pundit ruby-on-rails-5.2
ruby-on-rails pundit ruby-on-rails-5.2
edited Jan 22 at 18:32


E_net4
12.5k73772
12.5k73772
asked Jan 1 at 20:17
SuperBigLSuperBigL
15
15
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998634%2fauthorization-on-my-personal-blog-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998634%2fauthorization-on-my-personal-blog-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown