The problem that I'm having is that any instance variables that I declare in my show function of Charges Controller, appears as nil in the view. If I set an instance variable to a number, it doesn't even appear in the view. My home function works perfectly with instance variables.
This is my Controller with the Home and Show functions:
class ChargesController < ApplicationController
def home
@products = Product.all
end
def show
@product = Product.find_by(id: params[:id])
end
This is my View for the Show function(show.html.erb):
<h1>Product Details</h1>
<h3> <%= @product.name %> </h3>
<h4> <%= @product.price %> </h4>
<h5> <%= @product.description %> </h5>
This is my view for the Home function that contains the button that directs to the show page(home.html.erb)
<h1>Products</h1>
<% @products.each do |product| %>
<br>
<p> <%= product.name %>: $<%= product.price %> </p>
<p> <%= product.description %> </p>
<%= button_to "Product Details", charge_path(product.id), :method => "get" %>
<br>
<%end%>
find_by!method which will throw an exception if no product with that identifier could be found. You canrescue_fromthis to present an error. Assuming any database action succeeded is usually a mistake.@productswith@product? The only method executing on show will be the one that populates@product. If theshowaction successfully populates@productthen it will work in the view. If it doesn't, either some other bit of code reassigned the value or that code somehow didn't get executed in the first place.find_byhere, but since you're usingidas the parameter, try usingfind(params[:id])instead, to see what error it's throwing. By default, iffind_bydoesn't find what you're passing in, it simply returnsnil.findon the other hand will tell you what's wrong, perhapsRecordNotFound?find(params[:id])