1

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%>

error message I'm receiving

10
  • 1
    Try using the find_by! method which will throw an exception if no product with that identifier could be found. You can rescue_from this to present an error. Assuming any database action succeeded is usually a mistake. Commented Nov 3, 2016 at 1:04
  • But even if it exists, such as Product.first. That works if I set that to an instance variable in my home function, but not the show function. Still will show nil. I will use find_by! instead though, thank you. Commented Nov 3, 2016 at 1:11
  • Are you confusing @products with @product? The only method executing on show will be the one that populates @product. If the show action successfully populates @product then 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. Commented Nov 3, 2016 at 1:14
  • not sure if you have a preference towards using find_by here, but since you're using id as the parameter, try using find(params[:id]) instead, to see what error it's throwing. By default, if find_by doesn't find what you're passing in, it simply returns nil. find on the other hand will tell you what's wrong, perhaps RecordNotFound? Commented Nov 3, 2016 at 1:19
  • @iamjhu Still getting nil even if I use find(params[:id]) Commented Nov 3, 2016 at 1:30

3 Answers 3

2

find_by method returns nil if it does not find the record. So that means the product id(params[:id]) you searching in your Product model is not found.

You can check it manually in the rails console. Type rails c in your terminal then try the bellow code

Product.find_by(id: the_id_you_want_to_search)

Alternatively you can use find method. It raises an exception when it does not find the record.

def show
  @product = Product.find(params[:id])
end
Sign up to request clarification or add additional context in comments.

8 Comments

I've checked rails console and the products do exist
Then may be params[:id] of your show action getting wrong value somehow. Bdw, is it solved?
Nope, not solved yet. Still working on it. Still lost
Could you provide the error message as an screenshot?
No problem, I've added a screenshot in my original post
|
0

By default ,its always find by id,So kindly use only..

def show
    @product = Product.find params[:id]
end

Moreover,you can check in rails console using rails c to verify that the record is present with that id using,for example Product.find 18. Also check the url which should match with your routes..for example../users/:id for get request Only mapped to users#show action.

Get local copy of your routes using rake routes > path.text and use it as reference

3 Comments

Did you tried verifying the same id in rails console as i mentioned.Also verify your routes entry too.
Yes, a product exists for the id in rails console, my route is charges#show and /charges/:id for get request
undefined method `name' for nil:NilClass
0

In your home.html.erb try change this

<%= button_to "Product Details", charge_path(product.id), :method => "get" %>

to

<%= link_to "Product Details", charge_path(product.id) %>

Then if it does not work, try to raise the params or the instance in your show method

def show
  raise params.inspect
  @product = Product.find params[:id]
end

or

def show
  @product = Product.find params[:id]
  raise @product.inspect
end

Those were some simple ways to check whats happening in your code.

5 Comments

Nothing different is shown with these changes. Still the same error page, saying "undefined method `name' for nil:NilClass"
did you raise the instance in the controller? It should raise the params or instance. It seems there is a routing error. You are not getting into the show method.
Yes, I added raise @product.inspect too but no difference. In my routing I have resources :charges so it creates the show route automatically. Not sure where it's getting messed up
it is not messed up, maybe your just pointing incorrect paths, can you comment the output of rake routes | grep charge ? and also, is your view files under the correct directory? in this case, views/charges/show.html.erb ?
GET / charges#home charges GET /charges(.:format) charges#index POST /charges(.:format) charges#create new_charge GET /charges/new(.:format) charges#new edit_charge GET /charges/:id/edit(.:format) charges#edit charge GET /charges/:id(.:format) charges#show PATCH /charges/:id(.:format) charges#update PUT /charges/:id(.:format) charges#update DELETE /charges/:id(.:format) charges#destroy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.