【发布时间】:2015-12-13 10:46:56
【问题描述】:
如您所见,我有一个模型有点棘手的 rails 应用程序。应用能够为给定用户创建任务。
我正在使用下面的控制器/表单。显然,当我为 sby 创建任务时,我必须填写 :executor 字段,因为默认情况下我将成为 :assigner。一旦创建了任务,从我的角度来看,它将是一个 :assigned_task 。 下面的表单非常适用于 new/create 操作,并且 edit.html.erb 也使用正确的参数显示,但我收到错误(即使我尝试更改分配的任务而不是执行的任务):“没有路线匹配 [ PATCH] "/users/1/tasks"" 涉及更新操作。
我不确定是我的表单还是控制器出了问题。如果我点击 $rake 路线,一切看起来都很好。
我有 2 个问题: 1. 如何使更新操作适用于 :assigned_tasks,以便 :assigner 可以编辑他们分配给 sby 的任务? 2. 这是一个更难的问题:我应该怎么做才能让 :executors 能够编辑他们分配给的任务 (:executed_tasks)?
任务模型:
belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"
用户模型:
has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id"
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id"
新建和编辑表单:
<%= form_for @task, url: user_tasks_path do |f| %>
控制器:
def new
@user = current_user
@task = Task.new
end
def create
@user = current_user
@task = Task.new(task_params)
if @task.save
flash[:success] = "Task saved!"
redirect_to user_tasks_path(current_user)
else
render action: :new
end
end
def edit
@user = current_user
@task = Task.find(params[:id])
end
def update
@user = current_user
@task = @user.task.find(params[:id])
if @task.update_attributes(task_params)
flash[:success] = "Task updated!"
redirect_to user_tasks_path(current_user)
else
render action: :edit
end
end
private
def task_params
params.require(:task).permit(:executor_id, :name, :content, :deadline).merge(assigner_id: current_user.id)
end
路线
Prefix Verb URI Pattern Controller#Action
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_about GET /about(.:format) static_pages#about
static_pages_help GET /help(.:format) static_pages#help
static_pages_privacypolicy GET /privacypolicy(.:format) static_pages#privacypolicy
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_confirmation POST /users/confirmation(.:format) devise/confirmations#create
new_user_confirmation GET /users/confirmation/new(.:format) devise/confirmations#new
GET /users/confirmation(.:format) devise/confirmations#show
user_profile POST /users/:user_id/profile(.:format) profiles#create
new_user_profile GET /users/:user_id/profile/new(.:format) profiles#new
edit_user_profile GET /users/:user_id/profile/edit(.:format) profiles#edit
GET /users/:user_id/profile(.:format) profiles#show
PATCH /users/:user_id/profile(.:format) profiles#update
PUT /users/:user_id/profile(.:format) profiles#update
DELETE /users/:user_id/profile(.:format) profiles#destroy
complete_user_task PATCH /users/:user_id/tasks/:id/complete(.:format) tasks#complete
uncomplete_user_task PATCH /users/:user_id/tasks/:id/uncomplete(.:format) tasks#uncomplete
incoming_tasks_user_tasks GET /users/:user_id/tasks/incoming_tasks(.:format) tasks#incoming_tasks
outgoing_tasks_user_tasks GET /users/:user_id/tasks/outgoing_tasks(.:format) tasks#outgoing_tasks
completed_tasks_user_tasks GET /users/:user_id/tasks/completed_tasks(.:format) tasks#completed_tasks
user_tasks GET /users/:user_id/tasks(.:format) tasks#index
POST /users/:user_id/tasks(.:format) tasks#create
new_user_task GET /users/:user_id/tasks/new(.:format) tasks#new
edit_user_task GET /users/:user_id/tasks/:id/edit(.:format) tasks#edit
user_task GET /users/:user_id/tasks/:id(.:format) tasks#show
PATCH /users/:user_id/tasks/:id(.:format) tasks#update
PUT /users/:user_id/tasks/:id(.:format) tasks#update
DELETE /users/:user_id/tasks/:id(.:format) tasks#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / static_pages#home
【问题讨论】:
-
<%= form_for @task, url: user_tasks_path do |f| %>不适合更新。如果您想将其用于创建和更新,请尝试<%= form_for @task do |f| %>。 -
我会做两个控制器——一个assigned_tasks控制器和一个executed_tasks控制器。然后您可以根据用户的角色正确限制访问。
-
如果我使用没有 url 的表单,那么它甚至不会加载 edit.html.erb 视图。
-
这是为什么呢? form_for 方法会根据对象计算出 url 和方法。如果它没有正确执行此操作,则实例变量会发生其他事情。
-
发布了我的路线。我不完全明白为什么这个有一个路径:user_profile POST /users/:user_id/profile(.:format) profiles#create UNLIKE 这个:POST /users/:user_id/tasks(.:format) tasks#创建
标签: ruby-on-rails forms ruby-on-rails-4 model-view-controller crud