这是我的错误:无法找到没有ID的任务
我有这个协会:
TASK has_many :appointments
appointment belongs_to :task
路线:
resources :tasks do
resources :appointments
end
在任务节目中,(我使用部分btw)我有一个链接来创建新的约会页面:
<%= link_to new_appointment_path(id: task.id) %>
在约会控制器中:
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
问题在于创建动作中的这一行:
@task = Task.find(params[:id])
有人可以帮我吗?
RAKE OUTPUT 任务#指数 POST /tasks(.:format) 任务#创建 new_task GET /tasks/new(.:format) 任务#新 edit_task GET /tasks/:id/edit(.:format) 任务#编辑 任务GET /tasks/:id(.:format) 任务#秀 PATCH /tasks/:id(.:format) 任务#更新 PUT /tasks/:id(.:format) 任务#更新 DELETE /tasks/:id(.:format) 任务#销毁 task_appointments GET /tasks/:task_id/appointments(.:format) 约会#指数 POST /tasks/:task_id/appointments(.:format) 约会#创建 new_task_appointment GET /tasks/:task_id/appointments/new(.:format) 约会#新 edit_task_appointment GET /tasks/:task_id/appointments/:id/edit(.:format 约会#编辑 task_appointment GET /tasks/:task_id/appointments/:id(.:format) 约会#秀 PATCH /tasks/:task_id/appointments/:id(.:format) 约会#更新 PUT /tasks/:task_id/appointments/:id(.:format) 约会#更新 DELETE /tasks/:task_id/appointments/:id(.:format) 约会#销毁 GET /tasks(.:format) 任务#指数 POST /tasks(.:format) 任务#创建 GET /tasks/new(.:format) 任务#新 GET /tasks/:id/edit(.:format) 任务#编辑 GET /tasks/:id(.:format) 任务#秀 PATCH /tasks/:id(.:format) 任务#更新 PUT /tasks/:id(.:format) 任务#更新 DELETE /tasks/:id(.:format) 任务#破坏
的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
任务控制器:
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
答案 0 :(得分:1)
应该是:task_id
(指定Task
&#39; s id
),而不是:id
(指定Appointment
&#39; s {{ 1}}):
id
.erb:
@task = Task.find(params[:task_id])
routes.rb :
<%= link_to new_task_appointment_path (@task) %>
<%= simple_form_for [@task, @appointment], html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group do |b| %>
resources :tasks do
resources :responses, :appointments, :subcomments
end
不应拥有自己的资源。