如何将 user_id 传递给nested_resource 模型?

发布于 2024-12-27 17:18:00 字数 1811 浏览 1 评论 0原文

我有一个带有 devise 和 cancan 的小项目设置。有用户模型、项目模型、责任模型和任务模型。项目有嵌套任务。每个项目都分配给一个或多个用户。任务模型有名称、user_id 和project_id。身份验证和授权按预期工作。

添加新任务时(仅输入名称),project_id 会自动传递到模型/表(我认为这是因为路由),但不是 user_id。

我是否必须在hidden_​​field中传递user_id,或者是否可以在before过滤器中设置它?

有人可以提示如何在任务控制器中设置 user_id 吗?

谢谢

# Routes

resources :projects do
  resources :tasks
end

#Models

class User < ActiveRecord::Base
  has_many :responsibilities, :dependent => :destroy 
  has_many :projects, :through => :responsibilities 
  has_many :tasks, :dependent => :destroy 
...

class Project < ActiveRecord::Base
  has_many :tasks, :dependent => :destroy 
...

class Task < ActiveRecord::Base
  belongs_to :project
  belongs_to :user 
...

# Tasks Controller with all Task.find/new/update/... 
# methods removed like explained in cancan manual

class TasksController < ApplicationController
  load_and_authorize_resource :project
  load_and_authorize_resource :task, :through => :project
  ...
  def create
    respond_to do |format|
      if @task.save
        format.html { redirect_to [@project, @task], notice: 'created.' }
      else
        format.html { render action: "new" }
      end
  end

# Task Form View
<%= semantic_form_for [@project, @task] do |f| %>
  <%= f.inputs do %>
    <%= f.input :name %>
  <% end %>
  <%= f.actions %>
<% end %>

更新

它似乎可以与之前的过滤器一起使用。这是正确的方法吗?

class TasksController < ApplicationController
  before_filter :set_user, :only => [:create]
  load_and_authorize_resource :client
  load_and_authorize_resource :task, :through => :client, :shallow => true
  ...
  def set_user()
    params[:task][:user_id] = current_user.id.to_s
  end
  ...

I have a small project setup with devise and cancan. There are User, Project, Responsible and Task Models. Project has nested Tasks. Each Project is assigned to one or multiple users. The task model has a name, user_id and project_id. Authentication and Authorization is working like expected.

When adding a new Task (only an input for name) the project_id gets automatically passed to model/table (i think this is because of routing) but not the user_id.

Do i have to pass the user_id in a hidden_field or is it somehow possible to set this in a before filter?

Can somebody give a hint on howto set user_id in taskcontroller?

Thanks

# Routes

resources :projects do
  resources :tasks
end

#Models

class User < ActiveRecord::Base
  has_many :responsibilities, :dependent => :destroy 
  has_many :projects, :through => :responsibilities 
  has_many :tasks, :dependent => :destroy 
...

class Project < ActiveRecord::Base
  has_many :tasks, :dependent => :destroy 
...

class Task < ActiveRecord::Base
  belongs_to :project
  belongs_to :user 
...

# Tasks Controller with all Task.find/new/update/... 
# methods removed like explained in cancan manual

class TasksController < ApplicationController
  load_and_authorize_resource :project
  load_and_authorize_resource :task, :through => :project
  ...
  def create
    respond_to do |format|
      if @task.save
        format.html { redirect_to [@project, @task], notice: 'created.' }
      else
        format.html { render action: "new" }
      end
  end

# Task Form View
<%= semantic_form_for [@project, @task] do |f| %>
  <%= f.inputs do %>
    <%= f.input :name %>
  <% end %>
  <%= f.actions %>
<% end %>

Update

It seems to work with a before filter. Is this the right way?

class TasksController < ApplicationController
  before_filter :set_user, :only => [:create]
  load_and_authorize_resource :client
  load_and_authorize_resource :task, :through => :client, :shallow => true
  ...
  def set_user()
    params[:task][:user_id] = current_user.id.to_s
  end
  ...

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

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

发布评论

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

评论(1

£噩梦荏苒 2025-01-03 17:18:00

如果您正在使用 devise,您可以传入创建操作,您的 user_id

  def create
     @task.user = current_user
    respond_to do |format|
      if @task.save
        format.html { redirect_to [@project, @task], notice: 'created.' }
      else
        format.html { render action: "new" }
      end
  end

不确定这是否是您的意思

if you are using devise you can just pass in the create action you user_id

  def create
     @task.user = current_user
    respond_to do |format|
      if @task.save
        format.html { redirect_to [@project, @task], notice: 'created.' }
      else
        format.html { render action: "new" }
      end
  end

not quiet sure if this is what you mean

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文