I do this in my edit, update, destroy functions
@post = Post.friendly.find(params[:id])
if @post.user_id == current_user.id
Is there any way to optimize and make a single function, before I did it this way:
before_action :set_user_post, only: [:edit, :update, :destroy]
private
def set_user_post
@post = current_user.posts.find_by(id: params[:id])
end
But add the friendly_id gem and modify it like this:
private
def set_user_post
@post = current_user.posts.friendly.find(id: params[:id])
end
But it gives me an error.
The error is generated because you are using
find(id: params[:id])
when it should befind(params[:id])
.Solution