Finding one db entry and how to display it in a view

Here is my controller:

class BrowseController < ApplicationController
def index
@products = Product.find(:all)
end

def item
@products = Product.find(:all, :conditions => “id = #{params[:id]}”)
end
end


the ‘item’ method is bothering me, because I know I should be able to
simply do:

def item
@product = Product.find(params[:id])
end

but then I don’t know how to access it in the view other than,

<% for column in Product.content_columns %>

which I don’t want to do, I want to be able to call @product.name,
@product.etc, directly… Any ideas?

TIA

If your controller is and you do have a param named id:

def item

@product = Product.find(params[:id])

end

Then you can get attributes for your @product object in your view:

<%= @product.name %>

Assuming name is a valid attribute and you actually found a product
object
in the controller. You may want to put this in your view to see that you
actually found anything:

<%= debug( @product ) %>

HTH,
Jeff

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs