如何将 user_id 传递给nested_resource 模型?
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在使用 devise,您可以传入创建操作,您的 user_id
不确定这是否是您的意思
if you are using devise you can just pass in the create action you user_id
not quiet sure if this is what you mean