will willis
2006-Jun-22 19:51 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Jeff Everett
2006-Jun-22 20:22 UTC
[Rails] finding one db entry and how to display it in a view
If your controller is and you do have a param named id: def item> @product = Product.find(params[:id]) > endThen 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 On 6/22/06, will willis <silent11@gmail.com> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060622/04a84541/attachment.html