내 앱은 누구나 목록을 볼 수는 있지만 삭제 또는 편집 할 수는 없습니다.이 작업을 시도하면 오류 메시지가 나타납니다. 죄송합니다,이 리스팅은 다른 사람에게 속합니다.레일에서 사용자 권한을 올바르게 설정할 수 없습니다.
그러나 기존 사용자가 만든 목록을 편집하거나 삭제하려고하면 편집 화면으로 이동하거나 삭제 메시지가 표시되지만 동일한 오류 메시지가 표시됩니다. 죄송합니다.이 목록은 다른 사용자의 소유입니다. .
PostController.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
before_filter :check_user, only: [:edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
@post.user_id = current_user.id
if @post.save
redirect_to @post, notice: 'Post was successfully created.'
else
render action: 'new'
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
if @post.update(post_params)
redirect_to @post, notice: 'Post was successfully updated.'
else
render action: 'edit'
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
redirect_to posts_url
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:company, :contact, :email, :telephone, :website)
end
def check_user
if current_user != @post.user_id
redirect_to posts_url, alert: "Sorry, this contact belongs to someone else but you can view detalis by clicking on show"
end
end
end
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :name, presence: true
has_many :listings, dependent: :destroy
end
post.rb
class Post < ActiveRecord::Base
validates :company, :contact, :email, :telephone, :website, presence: true
belongs_to :posts
end
예 지금은 잘 작동하지 않지만 cancan에는 익숙하지 않지만 시원하게 들립니다. 감사합니다. – Neil