【问题标题】:Parent id could not be accessed from child controller create action无法从子控制器创建操作访问父 ID
【发布时间】:2015-07-04 13:14:07
【问题描述】:

这是我的错误:找不到没有 ID 的任务

我有这个关联:

TASK has_many :appointments
appointment belongs_to :task

路线:

resources :tasks do
   resources :appointments
end

在任务展示中,(我使用部分顺便说一句)我有一个链接可以创建新的约会页面:

<%= 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 输出 任务#index POST /tasks(.:format) 任务#创建 new_task GET /tasks/new(.:format) 任务#新 edit_task GET /tasks/:id/edit(.:format) 任务#编辑 任务 GET /tasks/:id(.:format) 任务#显示 补丁 /tasks/:id(.:format) 任务#更新 PUT /tasks/:id(.:format) 任务#更新 删除 /tasks/:id(.:format) 任务#destroy task_appointments GET /tasks/:task_id/appointments(.:format) 约会#index 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) 约会#show PATCH /tasks/:task_id/appointments/:id(.:format) 约会#更新 PUT /tasks/:task_id/appointments/:id(.:format) 约会#更新 删除 /tasks/:task_id/appointments/:id(.:format) 约会#destroy 获取 /tasks(.:format) 任务#index POST /tasks(.:format) 任务#创建 获取 /tasks/new(.:format) 任务#新 获取 /tasks/:id/edit(.:format) 任务#编辑 获取 /tasks/:id(.:format) 任务#显示 补丁 /tasks/:id(.:format) 任务#更新 PUT /tasks/:id(.:format) 任务#更新 删除 /tasks/:id(.:format) 任务#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

任务控制器:

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

【问题讨论】:

  • @task = Task.where(:id => params[:id]) 怎么样?它可能有效,也可能无效,但我想传达的是为什么不使用 where 子句而不是 find?

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

应该是:task_id(指定Taskid),而不是:id(指定Appointmentid):

@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 do |b| %>

routes.rb

resources :tasks do
  resources :responses, :appointments, :subcomments
end

:appointments 不应该拥有自己的资源。

【讨论】:

  • 创建操作中的这一行:'@task = Task.find(params[:id])'。它抱怨它找不到没有ID的任务,但我设置了它。我的路由似乎正确,但它给了我这个错误
  • @jumbo_siopao :您应该在 @task = Task.find(params[:task_id]) 内搜索 Appointment 控制器。
  • 这正是我的自动取款机。你说的我都做了。但是当我进入新页面时,它会导致同样的错误:找不到没有 ID 的任务
  • @jumbo_siopao 在帖子中添加您的路线(仅涉及约会和任务)
  • 对不起,在帖子中添加路线是什么意思。感谢您回复顺便说一句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-15
  • 1970-01-01
  • 2018-01-16
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多