2016-11-16 24 views
0

我試圖在創建列表時更改重定向,但我一直收到No route matches {:action=>"manage_photos", :controller=>"listings"} missing required keys: [:id]。我錯過了什麼?我的路線是嵌套的,在我的控制器中有一個manage_photos方法。不知道該從哪裏出發。創建方法不良重定向 - 導軌4

的routes.rb

resources :listings do 
    member do 
    get 'like' 
    get 'unlike' 
    get 'duplicate' 
    get 'gallery' 
    delete 'gallery' => 'listings#clear_gallery' 
    get 'manage_photos' 
    get 'craigslist' 
    get "add_to_collection" 
    get 'request_photos' 
    get 'rsvp' 
    end 
end 

耙路線:

manage_photos_listing GET /listings/:id/manage_photos(.:format) listings#manage_photos 

listings_controller:

創建方法:

def create 
    @listing = Listing.new(listing_params) 

    respond_to do |format| 
    if @listing.save 
     format.html { redirect_to manage_photos_listing_path, notice: 'Listing was successfully created.' } 
     format.json { render json: @listing, status: :created, location: @listing } 
    else 
     format.html { render action: "new", notice: "Correct the mistakes below to create the new listing" } 
     format.json { render json: @listing.errors, status: :unprocessable_entity } 
    end 
    end 
end 

manage_photos方法:

def manage_photos 
    @listing = Listing.find(params[:id]) 
end 

錯誤:

enter image description here

回答

4

因爲它是你必須添加的父元素的ID的嵌套的資源。嘗試:

manage_photo_listing(@listing)

所以它可以實際使用的@listing id和建設路線

+1

我不知道如果我能做到這一點的控制器。感謝您的幫助,效果很好! –