ruby on rails - Parent id could not be accessed from child controller create action -
this error: couldn't find task without id
i have association:
task has_many :appointments appointment belongs_to :task
routes:
resources :tasks resources :appointments end
in task show, (im using partial btw) have link 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 line in create action:
@task = task.find(params[:id])
can me please?
rake output tasks#index post /tasks(.:format) tasks#create new_task /tasks/new(.:format) tasks#new edit_task /tasks/:id/edit(.:format) tasks#edit task /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 /tasks/:task_id/appointments(.:format) appointments#index post /tasks/:task_id/appointments(.:format) appointments#create new_task_appointment /tasks/:task_id/appointments/new(.:format) appointments#new edit_task_appointment /tasks/:task_id/appointments/:id/edit(.:format appointments#edit task_appointment /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 /tasks(.:format) tasks#index post /tasks(.:format) tasks#create /tasks/new(.:format) tasks#new /tasks/:id/edit(.:format) tasks#edit /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' # 'catalogue' => 'catalogue#show' 'about' => 'static_pages#about' 'signup' => 'static_pages#signup' 'registration' => 'users#new' 'login' => 'sessions#new' post 'login' => 'sessions#create' delete 'logout' => 'sessions#destroy' resources :users member :clients, :workers end end resources :catalogues collection match 'search' => 'catalogues#search', via: [:get, :post], as: :search end end resources :tasks resources :responses end resources :tasks resources :appointments end resources :responses 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 not able 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 start_at minus 2 hours @expiry_time = @current_appointment.start_at - 2.hours @current_time = time.zone.now + 1.hours @responses.each |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
it should :task_id
(specifies task
's id
), not :id
(specifies appointment
's id
) :
@task = task.find(params[:task_id])
.erb :
<%= link_to new_task_appointment_path (@task) %> <%= simple_form_for [@task, @appointment], html: {class: 'form-horizontal'}, wrapper: :horizontal_input_group |b| %>
routes.rb :
resources :tasks resources :responses, :appointments, :subcomments end
:appointments
shouldn't have it's own resources.
Comments
Post a Comment