Reputation: 10673
I have a simple scenario that is proving difficult for some strange reason - I'm sure I'm making a simple mistake.
I have a GuestsController that I need to define my own actions and routes for. It only has one action at the moment because I can't get round this weird problem. Here is the action:
def booking
@booking = Booking.where(:friendly_id => params[:friendly_id])
end
The route that produces the param value is:
match '/:friendly_id', to: 'guests#booking', via: 'get'
and this is close to the end of my routes.rb
so in my view booking.html.rb I want to show the id of the booking relating to the friendly_id given
<%= @booking.id %>
and i get an error: NoMethodError in Guests#booking
Anyone got an idea why this is happening as it's giving me a headache.
Upvotes: 1
Views: 66
Reputation: 7937
The problem here is that you don't retrieve a Booking, you generate an ActiveRecord::Relation :
@booking = Booking.where(:friendly_id => params[:friendly_id])
# should be
@booking = Booking.where(:friendly_id => params[:friendly_id]).first
# or
@booking = Booking.find_by(:friendly_id => params[:friendly_id])
Upvotes: 1