使用嵌套资源Rails创建和更新重定向4

时间:2016-09-23 06:28:29

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

为了避免深层嵌套路线,我已经完成了以下工作:

的routes.rb

  resources :clients, controller: 'clients' do
    resources :sites, controller: 'clients/sites', except: [:index]
  end

  resources :sites, controller: 'clients/sites', except: [:index] do
    resources :site_contacts, controller: 'sites/site_contacts', except: [:index]
  end

我现在的问题是在我创建一个新的站点联系人之后我收到一条错误说明

undefined method `site_contact_url' for #<Sites::SiteContactsController:0x007f9dcb980100>
Did you mean?  site_contact_params

我的关联都设置正确,但我不知道如何处理网站联系创建和更新方法中的重定向(下方)

site_contacts_controller.rb

  def create
    @site = Site.friendly.find(params[:site_id])
    @site_contact = SiteContact.new(site_contact_params)
    @site_contact.site = @site

    respond_to do |format|
      if @site_contact.save
        format.html { redirect_to @site_contact, notice: 'Site contact was successfully created.' }
        format.json { render :show, status: :created, location: @site_contact }
      else
        format.html { render :new }
        format.json { render json: @site_contact.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @site = Site.friendly.find(params[:site_id])
    respond_to do |format|
      if @site.update(site_contact_params)
        format.html { redirect_to @site_contact, notice: 'Site contact was successfully updated.' }
        format.json { render :show, status: :ok, location: @site_contact }
      else
        format.html { render :edit }
        format.json { render json: @site_contact.errors, status: :unprocessable_entity }
      end
    end
  end

我最初在创建和更新方法中将@site_contact更改为@site并且它抛出错误,我现在将它们更改回原始版本,因为它显然不起作用。

我迷失在这里,并且正在努力寻找解决方案。任何帮助将不胜感激!

如果您需要任何进一步的详细信息,请提前致谢。

  

编辑#1:为网站添加rake路由输出

     site_site_contacts POST   /sites/:site_id/site_contacts(.:format)                  sites/site_contacts#create
  new_site_site_contact GET    /sites/:site_id/site_contacts/new(.:format)              sites/site_contacts#new
 edit_site_site_contact GET    /sites/:site_id/site_contacts/:id/edit(.:format)         sites/site_contacts#edit
      site_site_contact GET    /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#show
                        PATCH  /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#update
                        PUT    /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#update
                        DELETE /sites/:site_id/site_contacts/:id(.:format)              sites/site_contacts#destroy

以下是客户端/网站rake路由输出

       client_sites POST   /clients/:client_id/sites(.:format)                      clients/sites#create
    new_client_site GET    /clients/:client_id/sites/new(.:format)                  clients/sites#new
   edit_client_site GET    /clients/:client_id/sites/:id/edit(.:format)             clients/sites#edit
        client_site GET    /clients/:client_id/sites/:id(.:format)                  clients/sites#show
                    PATCH  /clients/:client_id/sites/:id(.:format)                  clients/sites#update
                    PUT    /clients/:client_id/sites/:id(.:format)                  clients/sites#update
                    DELETE /clients/:client_id/sites/:id(.:format)                  clients/sites#destroy

所以我确实在输出中看到了一个问题,它应该重定向到client /:client_id / sites /:site_id / site_contacts /:id,因为没有站点控制器,或者没有站点/显示它嵌套到客户端

  

编辑#2新错误

No route matches {:action=>"show", :controller=>"sites/site_contacts", :site_id=>#<SiteContact id: 3, site_id: 1, name: "Shawn Wilson", tel_number: "403-615-5915", tel_ext: "", email: "swilson@taurenltd.com", contact_type: "Management", after_hours: true, notes: "Primary", created_at: "2016-09-23 06:47:42", updated_at: "2016-09-23 06:47:42", slug: "shawn-wilson-b7a18908-34ae-4385-bcf7-17c97547dabb">} missing required keys: [:id]

0 个答案:

没有答案