0

我有一个Template模型,一个Doc模型。它们是嵌套资源,Templates是父资源,因此:

resources :templates do
  get "/documents/lock/:id" => "docs#lock", :as => :lock_doc
  get "/documents/unlock/:id" => "docs#unlock", :as => :unlock_doc
  get "/documents/pdf/:id" => "docs#pdf", :as => :pdf_doc
  resources :docs, :path => :documents
end

我认为那部分一切正常。当我尝试提交用于创建doc记录的表单时,但出现路由错误,因此:

ActionController::RoutingError (No route matches {:action=>"edit", :controller=>"docs", :template_id=>nil, :id=>#<Doc id: 2, user_id: "admin", cover: "1209hpnl", message: "The world economic outlook is improving, albeit slo...", created_at: "2013-01-07 03:54:05", updated_at: "2013-01-07 03:54:05", issue_code: "1209hpnl", title: "January 2013", locked: nil, retired: "active", template: nil>}):
app/controllers/docs_controller.rb:134:in `block (2 levels) in create'
app/controllers/docs_controller.rb:132:in `create'

这些行对应于create方法:

def create
  @doc = Doc.new(params[:doc])
  respond_to do |format|
    if @doc.save
      format.html { redirect_to share_url(@doc), notice: "Saved. You may from here #{view_context.link_to('edit', edit_template_doc_url(@doc))} it further, #{view_context.link_to('finalise', template_lock_doc_url(@doc))} it, or return #{view_context.link_to('home', root_url)}.".html_safe }
      format.json { render json: @doc, status: :created, location: @doc }
    else
      format.html { render action: "new" }
      format.json { render json: @doc.errors, status: :unprocessable_entity }
    end
  end
end

我认为问题出在此处,但我一生都无法弄清楚。

为任何帮助干杯!

编辑:与rake routes

    template_lock_doc GET    /templates/:template_id/documents/lock/:id(.:format)   docs#lock
  template_unlock_doc GET    /templates/:template_id/documents/unlock/:id(.:format) docs#unlock
     template_pdf_doc GET    /templates/:template_id/documents/pdf/:id(.:format)    docs#pdf
        template_docs GET    /templates/:template_id/documents(.:format)            docs#index
                      POST   /templates/:template_id/documents(.:format)            docs#create
     new_template_doc GET    /templates/:template_id/documents/new(.:format)        docs#new
    edit_template_doc GET    /templates/:template_id/documents/:id/edit(.:format)   docs#edit
         template_doc GET    /templates/:template_id/documents/:id(.:format)        docs#show
                      PUT    /templates/:template_id/documents/:id(.:format)        docs#update
                      DELETE /templates/:template_id/documents/:id(.:format)        docs#destroy
            templates GET    /templates(.:format)                                   templates#index
                      POST   /templates(.:format)                                   templates#create
         new_template GET    /templates/new(.:format)                               templates#new
        edit_template GET    /templates/:id/edit(.:format)                          templates#edit
             template GET    /templates/:id(.:format)                               templates#show
                      PUT    /templates/:id(.:format)                               templates#update
                      DELETE /templates/:id(.:format)                               templates#destroy
4

1 回答 1

1

问题出在您edit_template_doc_url(@doc)对通知字符串内部的调用中。您还需要提供模板,如下所示:

edit_template_doc_url(params[:template_id], @doc)
于 2013-01-07T05:44:11.510 回答