我有一个有点棘手模型的rails应用程序,如你所见。应用程序可以为给定用户创建任务。
我正在使用下面的控制器/表格。显然,当我为sby创建任务时,我必须填写:executor字段,因为我将默认为:assigner。创建任务后,从我的角度来看,它将是:assigned_task。 下面的表单适用于new / create操作,edit.html.erb也会显示正确的参数,但我收到错误(即使我尝试更改assigned_task而不是execution_task):“没有路由匹配[ PATCH]“/ users / 1 / tasks”“当谈到更新操作时。
我不确定这是我的表单还是控制器出错了。如果我点击$ rake路线,一切看起来都不错。
我有两个问题:1。如何使更新操作适用于:assigned_tasks,所以:assigner可以编辑他们分配给sby的任务吗? 2.这是一个更难的问题:我该怎么做:执行者能够编辑他们分配给的任务(: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
答案 0 :(得分:0)
我把它拉了下来。我仍然不明白为什么前一个没有成功,为什么会这样。我用它来用于user.profile。虽然配置文件与用户处于一对一的关系,但确实如此。
控制器:
def edit
@user = current_user
@task = Task.find(params[:id])
end
def update
@user = current_user
@task = 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
形式:
<%= form_for ([@user, @task]) do |f| %>