Mike Wiesenhart Mike Wiesenhart - 19 days ago 5
Ruby Question

Bad Redirect On Create Method - Rails 4

I am trying to change the redirect when I create a listing, but I keep getting a

No route matches {:action=>"manage_photos", :controller=>"listings"} missing required keys: [:id]
. What am I missing? My route is nested and there is a
manage_photos
method in my controller. Not sure where to go from here.

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


rake routes:

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


listings_controller:

create method:

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 method:

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


error:

enter image description here

Answer

Since it is a nested resource you have to add the id of the parent element. Try:

manage_photo_listing(@listing)

so it can actually use the id of @listing and build the route

Comments