This is my error: Couldn't find Task without an ID
I have this association:
TASK has_many :appointments
appointment belongs_to :task
Routes:
resources :tasks do
resources :appointments
end
In task show, (Im using a partial btw) I have a link to get to create new appointment page:
<%= link_to new_appointment_path(id: task.id) %>
In appointments controller:
def new
@task = Task.find(params[:id])
@appointment = @task.appointments.new
# @appointment = Appointment.new
end
def create
@task = Task.find(params[:id])
@appointment = @task.appointments.build(appointment_params)
if @appointment.save
redirect_to @task
else
render 'new'
end
end
The problem is this line in create action:
@task = Task.find(params[:id])
Can anyone help me please?
RAKE OUTPUT tasks#index POST /tasks(.:format) tasks#create new_task GET /tasks/new(.:format) tasks#new edit_task GET /tasks/:id/edit(.:format) tasks#edit task GET /tasks/:id(.:format) tasks#show PATCH /tasks/:id(.:format) tasks#update PUT /tasks/:id(.:format) tasks#update DELETE /tasks/:id(.:format) tasks#destroy task_appointments GET /tasks/:task_id/appointments(.:format) appointments#index POST /tasks/:task_id/appointments(.:format) appointments#create new_task_appointment GET /tasks/:task_id/appointments/new(.:format) appointments#new edit_task_appointment GET /tasks/:task_id/appointments/:id/edit(.:format appointments#edit task_appointment GET /tasks/:task_id/appointments/:id(.:format) appointments#show PATCH /tasks/:task_id/appointments/:id(.:format) appointments#update PUT /tasks/:task_id/appointments/:id(.:format) appointments#update DELETE /tasks/:task_id/appointments/:id(.:format) appointments#destroy GET /tasks(.:format) tasks#index POST /tasks(.:format) tasks#create GET /tasks/new(.:format) tasks#new GET /tasks/:id/edit(.:format) tasks#edit GET /tasks/:id(.:format) tasks#show PATCH /tasks/:id(.:format) tasks#update PUT /tasks/:id(.:format) tasks#update DELETE /tasks/:id(.:format) tasks#destroy
routes.rb
root 'static_pages#home'
# get 'catalogue' => 'catalogue#show'
get 'about' => 'static_pages#about'
get 'signup' => 'static_pages#signup'
get 'registration' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :clients, :workers
end
end
resources :catalogues do
collection do
match 'search' => 'catalogues#search', via: [:get, :post], as: :search
end
end
resources :tasks do
resources :responses
end
resources :tasks do
resources :appointments
end
resources :responses do
resources :subcomments
end
resources :appointments
resources :subcomments
resources :educations
resources :offered_services
resources :works
resources :worker_steps
resources :client_steps
Task conroller:
class TasksController < ApplicationController
before_action :logged_in_user_worker, only: [:new] #worker will not be able to acces tasks/new
def new
@task = current_user.task_posts.build
@task.appointments.build
end
def create
@task = current_user.task_posts.build(task_params)
if @task.save
flash[:success] = "Task created!"
redirect_to @task
else
render 'new'
end
end
def edit
@task = Task.find(params[:id])
@task.county_id = @task.county.id
@task.category_id = @task.category.id
end
def update
@task = Task.find(params[:id])
if @task.update_attributes(task_params)
flash[:success] = "Task updated"
redirect_to @task
else
render 'edit'
end
end
def show
@task = Task.find(params[:id])
@responses = @task.responses.all
@current_appointment = @task.appointments.first
#expiry time is start_at minus 2 hours
@expiry_time = @current_appointment.start_at - 2.hours
@current_time = Time.zone.now + 1.hours
@responses.each do |r|
if r.is_accepted == true
@accepted_offer = r
break
end
end
end
private
def task_params
params.require(:task).permit(:category_id, :subcategory_id, :title, :description, :pay_offer, :is_pay_per_hour, :county_id, :area_id,
appointments_attributes: [:id, :start_date, :start_time, :duration] )
end
def logged_in_user_worker
unless current_user.client?
redirect_to(root_url)
end
end
end