Reputation: 2380
I have a rails app with a bit tricky model as you see. App is able to create tasks for a given user.
I'm using the controller/form below. Obviously when I create task for sby I have to fill the :executor field since I'm gonna be the :assigner by default. Once the task is created, it's gonna be an :assigned_task from my point of view. The form below works perfectly for the new/create action and edit.html.erb gets displayed as well with the right parameters, but I get the error (even if I try to change an assigned_task not an executed_task): "No route matches [PATCH] "/users/1/tasks"" when it comes to the update action.
I'm not sure if it's my form or controller that goes wrong. If I hit $rake routes everything looks fine.
I got 2 questions: 1. How can I make the update action work for :assigned_tasks, so :assigner could edit the tasks they assigned to sby? 2. This is the harder question: What should I do to have :executors be able to edit as well the tasks they got assigned to (:executed_tasks)?
task model:
belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"
user model:
has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id"
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id"
Form for new and edit:
<%= form_for @task, url: user_tasks_path do |f| %>
Controller:
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
routes
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
Upvotes: 0
Views: 11980
Reputation: 2380
I pulled it off. I still don't get why the previous one didn't work out and why this does. I used it for user.profile. It's true though that the profile was in one_to_one relationship with the user.
controller:
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:
<%= form_for ([@user, @task]) do |f| %>
Upvotes: 0