I have in my orders/edit.html.erb a form that begins like this:
<%= simple_form_for [@reservation, @order] do |f| %>
Orders Controller:
def edit
@reservation = Reservation.find_by_id(params[:reservation_id])
@order = Order.find_by_id(params[:id])
end
Association:
Reservation :has_one Order
Routes:
resources :reservations do
resources :orders
end
If my path resembles something like /reservations/10/orders/10/edit I end up getting an error from Rails saying NoMethodError in Orders#edit and undefined method 'model_name' for nil:NilClass
When I create a new order the form works perfectly fine so not sure why i'm getting an error all of a sudden, can someone help me with this issue?
find_by_idtofind... could be wrong, but my guess is that the row does not exist; if so,findwill blow up (properly, as you don't want to continue in this case anyway);find_by_idwill just set things tonil, which will non-deterministically blow up somewhere along the way.find_by_idwon't ever blow up, it'll always returnnilif the record isn't found. But you can handle that case manually.