Cancan无法编辑或删除

时间:2015-05-27 07:07:32

标签: ruby-on-rails ruby-on-rails-4 cancan

使用Cancan时,我无法编辑或删除评论 - 评论与作业有关。

Cancan适用于Jobs,但对于Comments,编辑和删除不会显示。这是因为评论显示在工作中吗?



class Comment < ActiveRecord::Base
  belongs_to :job
  belongs_to :user
end

                                  
class Job < ActiveRecord::Base
	belongs_to :jobcategory
	has_many :comments, dependent: :destroy
end
&#13;
&#13;
&#13;

&#13;
&#13;
ActiveRecord::Schema.define(version: 20150522132410) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "comments", force: true do |t|
    t.text     "content"
    t.integer  "job_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "comments", ["job_id"], name: "index_comments_on_job_id", using: :btree
  add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree
&#13;
&#13;
&#13;

&#13;
&#13;
user ||= User.new # guest user (not logged in)
        if user.admin?
            can :access, :rails_admin       # only allow admin users to access Rails Admin
            can :dashboard
            can :manage, :all
        else
            can :read, :all
            can [ :edit, :update, :destroy ], Comment do |comment|
                comment.try(:user_id) == user.id
            end

            can [ :edit, :update, :destroy ], Job do |job|
                job.user_id == user.id
            end
        can :create , Comment
        can :create , Job
        end
&#13;
&#13;
&#13;

&#13;
&#13;
- if can? :update, @comment
	= link_to "Edit", edit_job_comment_path(comment.job, comment)

- if can? :destroy, @comment
	= link_to "Delete", [comment.job, comment], method: :delete, data: { confirm: "Are you sure?" }
&#13;
&#13;
&#13;

&#13;
&#13;
class JobsController < ApplicationController
	before_action :find_job, only: [:show, :edit, :update, :destroy]
	before_action :authenticate_user!,except:[:index]

	def show
		@comments =Comment.where(job_id: @job)
	end
&#13;
&#13;
&#13;

更新:

&#13;
&#13;
class CommentsController < ApplicationController
	before_action :authenticate_user!

	def show
	end

	def create
		@job = Job.find(params[:job_id])
		@comment = @job.comments.create(params[:comment].permit(:content))
		@comment.user_id = current_user.id if current_user
		@comment.save

		if @comment.save
			redirect_to job_path(@job)
		else
			render 'new'
		end
	end

	def edit
		@job = Job.find(params[:job_id])
		@comment = Comment.find(params[:id])
		authorize! :update, @comment
	end

	def update
		@job = Job.find(params[:job_id])
		@comment = @job.comments.find(params[:id])

		if @comment.update(params[:comment].permit(:comment))
			redirect_to job_path(@job)
		else
			render 'edit'
		end
		authorize! :update, @comment
	end

	def destroy
		@job = Job.find(params[:job_id])
		@comment = @job.comments.find(params[:id])
		@comment.destroy
		redirect_to job_path(@job)
		authorize! :destroy, @comment
	end
end
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您的对象似乎是评论,但您使用了 @comment

相关问题