简体   繁体   中英

Couldn't find Object with 'id'=

stumped here, probably due to my rails noobness.

I am building a 2 sided market place with bids. using devise for 2 user types relevant here, clients and guides. clients build a trip, guides can view trips and then bid on the trip. i've created a custom landing page for the guides (home) once they sign in which lists client trips, i use a custom view and method in my guides controller to do this, no problem.

however, when from that page i try to go to the next custom page for the guide to view the details of the trip (and then submit a bid), suddenly i get a "Couldn't find Trip with 'id'=" error related to the Trip.find(params[:id]) (if i hardcode Trip.find(1) it works).

guides controller

class GuidesController < ApplicationController
  def home
    @trips = Trip.all
    @guide = current_guide
  end

  def view_trip
    @trip = Trip.find(params[:id])
    @guide = current_guide
  end
end

guide's home page (which is where the guide clicks to view the trip)

<p>Guides's Email: <%= @guide.email %></p>
<% @trips.each do |trip| %>
  <li>Location: <%= trip.location.description %></li>
  <li>Details <%= trip.details %></li> 
  <p><%= link_to "View Details and Bid", guides_view_trip_path(trip) %></p>
<% end %>

and then the error output at the page when i try to visit it/click on the link: amazonaws.com/guides/view_trip.1

Couldn't find Trip with 'id'=
Extracted source (around line #21):

19  def view_trip
20
21      @trip = Trip.find(params[:id])
22      #@trip = Trip.find(1)
23      @guide = current_guide
24  end

Request
Parameters:
{"format"=>"1"}

Change this line:

<p><%= link_to "View Details and Bid", guides_view_trip_path(trip) %></p>

By

<p><%= link_to "View Details and Bid", guides_view_trip_path(id: trip.id) %></p>

I don't know what is your routes.rb design, if you need to take this URL with rails default RESTful URL then your routes.rb looks like this

get 'guides/view_trip/:id(.:format)', to: "guides#view_trip", as: :guides_view_trip

#=>  guides_view_trip GET  /guides/view_trip/:id(.:format) guides#view_trip

Now you can use the view like below

<%= link_to "View Details and Bid", guides_view_trip_path(trip) %>
#=> /guides/view_trip/1

If you use guides_view_trip_path(id: trip.id) then URL looks like this amazonaws.com/guides/view_trip?id=1 and if you use RESTful which I described then looks like this amazonaws.com/guides/view_trip/1

Or you can use Trip.find(params[:id]) to Trip.find(params[:format])

Hope it helps

Your code should be like that

class GuidesController < ApplicationController
  def home
    @trips = Trip.all
    @guide = current_guide
  end

  def view_trip
    @trip = Trip.find(params[:format])
    @guide = current_guide
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM