2015-05-24 29 views
0

我是Ruby on Rails的新手,任何幫助都會很多讚賞。我不確定如何解決我的錯誤。我想我的意思是在我的控制器做更多,但我很困惑,因爲這是我面臨的嵌套資源挑戰沒有路由匹配{:action =>「new」,:controller =>「adverts」,:userr_id => nil}缺少必需的密鑰:[:userr_id]

我想在我的意見,查看廣告(表演)的細節,但是當我點擊鏈接show我得到下面的錯誤:

No route matches {:action=>"new", :controller=>"adverts", :userr_id=>nil} missing required keys: [:userr_id] 

ActionController::UrlGenerationError (No route matches {:action=>"new", :controller=>"adverts", :userr_id=>nil} missing required keys: [:userr_id]): 
    app/views/shared/_header_recruiter.html.erb:6:in `_app_views_shared__header_recruiter_html_erb___2254802171992378619_70258749378740' 
    app/views/adverts/show.html.erb:1:in `_app_views_adverts_show_html_erb___3448815807687044417_70258749644120' 
    app/controllers/adverts_controller.rb:26:in `show' 

我有廣告下userr嵌套(招聘) - routes.rb中

Rails.application.routes.draw do 
    devise_for :userrs 
    resources :userrs do 
    resources :adverts 
    end 

    devise_for :userjs 
    root 'static_pages#homepg' 
    get  'search',  to: 'static_pages#searchpg' 
end 

我創建了一個靜態頁面稱爲搜索 - static_pages_controller.rb

class StaticPagesController < ApplicationController 
    respond_to :html, :xml, :json 

    def searchpg 
    @adverts = Advert.all 
    end 
end 

我在我的搜索視圖文件下面的值編碼 - searchpg.html.erb

<div> 
    <table> 
    <thead> 
     <tr> 
     <th>Published</th> 
     <th>Title</th> 
     <th>Content</th> 
     <th>City</th> 
     <th></th> 
     </tr> 
    </thead> 

    <tbody> 
     <% @adverts.each do |advert| %> 
     <tr> 
      <td><%= advert.created_at.strftime("%B %d, %Y") %></td> 
      <td><%= link_to advert.title, '#' %></td> 
      <td><%= advert.content %></td> 
      <td><%= advert.city %></td> 
      <td><%= link_to 'Show', userr_advert_path(advert.userr, advert) %></td> 
     </tr> 
     <% end %> 
    </tbody> 
    </table> 
</div> 

論文是我的路線:

userr_adverts GET /userrs/:userr_id/adverts(.:format)   adverts#index 
          POST /userrs/:userr_id/adverts(.:format)   adverts#create 
     new_userr_advert GET /userrs/:userr_id/adverts/new(.:format)  adverts#new 
     edit_userr_advert GET /userrs/:userr_id/adverts/:id/edit(.:format) adverts#edit 
      userr_advert GET /userrs/:userr_id/adverts/:id(.:format)  adverts#show 
          PATCH /userrs/:userr_id/adverts/:id(.:format)  adverts#update 
          PUT /userrs/:userr_id/adverts/:id(.:format)  adverts#update 
          DELETE /userrs/:userr_id/adverts/:id(.:format)  adverts#destroy 

我已經有一個廣告控制器:

class AdvertsController < ApplicationController 
    respond_to :html, :xml, :json 
    before_action :set_advert, only: [:show, :edit, :update, :destroy] 

    def index 
    @userr = Userr.find(params[:userr_id]) 
    @adverts = @userr.adverts.order("created_at DESC") 
    respond_with(@adverts) 
    end 

    def show 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.find(params[:id]) 
    respond_with(@advert) 
    end 

    def new 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.build 
    respond_with(@advert) 
    end 

    def edit 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.find(params[:id]) 
    end 

    def create 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.create(advert_params) 
    respond_to do |format| 
     if @advert.save 
     format.html { redirect_to([@advert.userr, @advert], notice: 'Advert was successfully created.') } 
     format.json { render json: @advert, status: :created, location: @advert } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @advert.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.find(params[:id]) 
    respond_to do |format| 
     if @advert.update_attributes(advert_params) 
     format.html { redirect_to([@advert.userr, @advert], notice: 'Advert was successfully updated.') } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @advert.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @userr = Userr.find(params[:userr_id]) 
    @advert = @userr.adverts.find(params[:id]) 
    @advert.destroy 

    respond_to do |format| 
     #1st argument reference the path /recruiters/:recruiter_id/adverts/ 
     format.html { redirect_to(recruiter_adverts_url) } 
     format.json { head :no_content } 
    end 
    end 

    private 
    def set_advert 
     @advert = Advert.find(params[:id]) 
    end 

    def advert_params 
     params.require(:advert).permit(:title, :content, :category_jobtype_id, :category_positiontype_id, :salarystart, :salaryend, :category_country_id, :city, :town, :postcode, :category_editorialapproval_id, :category_applicationrequest_id, :category_advert_id, :userr_id) 
    end 
end 
+1

看app/views/shared/_header_recruiter.html.erb:6。 嘗試使用'new_userr_advert(params [:userr_id])'而不是'new_userr_advert' –

+0

謝謝soo much Alex! – ARTLoe

回答

0

看起來好像你沒有advert.userr ob JECT。最簡單的方法來檢查打印它。

所以暫時刪除您link並打印userr

<% @adverts.each do |advert| %> <tr> <td><%= advert.created_at.strftime("%B %d, %Y") %></td> <td><%= link_to advert.title, '#' %></td> <td><%= advert.content %></td> <td><%= advert.city %></td> <td><%#= link_to 'Show', userr_advert_path(advert.userr, advert) %></td> <td><%= advert.userr.inspect %></td> </tr> <% end %> 所以,如果ü不能看到任何東西,這意味着userradvert之間的關係,你是不是設置正確。

HTH

0

錯誤通知中有這意味着廣告對象沒有userr對象,或者您可能忘記輸入userr_id而手動鍵入網址URL中有userr_id。

相關問題