2016-11-16 27 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

我不知道如果我能做到这一点的控制器。感谢您的帮助,效果很好! –