调试康康舞

发布于 2024-12-11 09:14:09 字数 1443 浏览 1 评论 0原文

我正在尝试调试以下块。

  can :create, Todo do |todo|
    todo.user.account == user.account
  end

我在 TodosController 顶部使用 load_and_authorize_resource

我遇到的问题是,在执行 :create 操作时,上述 todo.user.account 的“account”是一个 nil 方法。似乎待办事项对象在传递时并未完全实例化;我不知道如何控制这一点。

以下是 TodosController 的相关部分:

  def new
    #@todo = Todo.new
    @todo = current_user.todos.build
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @todo }
    end
  end

  def create
    #@todo = Todo.new(params[:todo])

    #Build the @todo object including the relations of who's made this todo.
    @todo = current_user.todos.build(params[:todo])
    @todo.subscriptions.build(:user => current_user, :todo => @todo)
    @todo.subscriptions.build(:user => @todo.assignee, :todo => @todo)
    respond_to do |format|
      if @todo.save
        #format.html { redirect_to @todo, :notice => 'Todo was successfully created.' }
        Notifier.notify_assignee(@todo,current_user).deliver
        format.html { redirect_to :back }
        format.json { render :json => @todo, :status => :created, :location => @todo }
      else
        format.html { render :action => "new" }
        format.json { render :json => @todo.errors, :status => :unprocessable_entity }
      end
    end
  end

I'm trying to debug the following block.

  can :create, Todo do |todo|
    todo.user.account == user.account
  end

I'm using load_and_authorize_resource at the top of TodosController.

The problem I'm having is that the 'account' is a nil method for the above todo.user.account when performing the :create action. It seems the todo object isn't fully instantiated when it's passed; I'm not sure how to control this bit.

Here are the relevant sections of the TodosController:

  def new
    #@todo = Todo.new
    @todo = current_user.todos.build
    respond_to do |format|
      format.html # new.html.erb
      format.json { render :json => @todo }
    end
  end

  def create
    #@todo = Todo.new(params[:todo])

    #Build the @todo object including the relations of who's made this todo.
    @todo = current_user.todos.build(params[:todo])
    @todo.subscriptions.build(:user => current_user, :todo => @todo)
    @todo.subscriptions.build(:user => @todo.assignee, :todo => @todo)
    respond_to do |format|
      if @todo.save
        #format.html { redirect_to @todo, :notice => 'Todo was successfully created.' }
        Notifier.notify_assignee(@todo,current_user).deliver
        format.html { redirect_to :back }
        format.json { render :json => @todo, :status => :created, :location => @todo }
      else
        format.html { render :action => "new" }
        format.json { render :json => @todo.errors, :status => :unprocessable_entity }
      end
    end
  end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-12-18 09:14:09
load_and_authorize_resource :except => :create

进而:

def create
  #@todo = Todo.new(params[:todo])

  #Build the @todo object including the relations of who's made this todo.
  @todo = current_user.todos.build(params[:todo])
  authorize! :create, @todo
load_and_authorize_resource :except => :create

and then:

def create
  #@todo = Todo.new(params[:todo])

  #Build the @todo object including the relations of who's made this todo.
  @todo = current_user.todos.build(params[:todo])
  authorize! :create, @todo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文