I get Error after creating product

I get the following error inside my browser: ActiveRecord::RecordNotFound in ProductsController#show

Couldn't find Product without an ID

RAILS_ROOT: /Users/musdev/work/depot Application Trace | Framework Trace | Full Trace

/Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1567:in `find_from_ids' /Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find' /Users/musdev/work/depot/app/controllers/products_controller.rb:17:in `show'

Request

Parameters:

{"id"=>"2"}

The code on line 17 in my controller looks like this

def show     @product = Product.find(params[:id => "show"])

    respond_to do |format|       format.html {render show.html.erb}       format.xml #{ render :xml => @product }     end   end

I'm new to rails and i cannot pinpoint where my issue is occurring Can someone please give me a hand

Thank you

Musdev Musdev wrote:

I get the following error inside my browser: ActiveRecord::RecordNotFound in ProductsController#show

Couldn't find Product without an ID

Request

Parameters:

{"id"=>"2"}

The code on line 17 in my controller looks like this

def show     @product = Product.find(params[:id => "show"])

    respond_to do |format|       format.html {render show.html.erb}       format.xml #{ render :xml => @product }     end   end

I'm new to rails and i cannot pinpoint where my issue is occurring Can someone please give me a hand

Thank you

Hi, Your code should be like this, def show      @product = Product.find(params[:id])

     respond_to do |format|        format.html {render show.html.erb}        format.xml #{ render :xml => @product }      end end

Thanks, Anubhaw

Anubhaw Prakash wrote:

Musdev Musdev wrote:

I get the following error inside my browser: ActiveRecord::RecordNotFound in ProductsController#show

Couldn't find Product without an ID

Request

Parameters:

{"id"=>"2"}

The code on line 17 in my controller looks like this

def show     @product = Product.find(params[:id => "show"])

    respond_to do |format|       format.html {render show.html.erb}       format.xml #{ render :xml => @product }     end   end

I'm new to rails and i cannot pinpoint where my issue is occurring Can someone please give me a hand

Thank you

Hi, Your code should be like this, def show      @product = Product.find(params[:id])

     respond_to do |format|        format.html {render show.html.erb}        format.xml #{ render :xml => @product }      end end

Thanks, Anubhaw

Hey Anubhaw,

Thank you, I tried that but now I get this error message:

SystemStackError in ProductsController#show

stack level too deep

RAILS_ROOT: /Users/musdev/work/depot Application Trace | Framework Trace | Full Trace

/Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1689:in `' /Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1689:in `construct_finder_sql' /Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1548:in `find_every' /Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1583:in `find_one' /Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1569:in `find_from_ids' /Users/musdev/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find' /Users/musdev/work/depot/app/controllers/products_controller.rb:17:in `show' /Users/musdev/work/depot/app/controllers/products_controller.rb:20:in `show' /Users/musdev/work/depot/app/controllers/products_controller.rb:19:in `show'

Here is what my controller code looks like:

class ProductsController < ApplicationController   # GET /products   # GET /products.xml   def index     @products = Product.new     @products = Product.all

    respond_to do |format|       format.html # index.html.erb       format.xml { render :xml => @products }     end   end

  # GET /products/1   # GET /products/1.xml   def show     @products = Product.find(params[:id])

    respond_to do |format|       format.html {render show.html.erb}       format.xml #{ render :xml => @product }     end   end

  # GET /products/new   # GET /products/new.xml   def new     @product = Product.new

    respond_to do |format|       format.html # new.html.erb       format.xml { render :xml => @product }     end   end

  # GET /products/1/edit   def edit     @product = Product.find(params[:id])   end

  # POST /products   # POST /products.xml   def create     @product = Product.new(params[:product])

    respond_to do |format|       if @product.save         flash[:notice] = 'Product was successfully created.'         format.html { redirect_to(@product) }         format.xml { render :xml => @product, :status => :created, :location => @product }       else         format.html { render :action => "new" }         format.xml { render :xml => @product.errors, :status => :unprocessable_entity }       end     end   end

  # PUT /products/1   # PUT /products/1.xml   def update     @product = Product.find(params[:id])

    respond_to do |format|       if @product.update_attributes(params[:product])         flash[:notice] = 'Product was successfully updated.'         format.html { redirect_to(@product) }         format.xml { head :ok }       else         format.html { render :action => "edit" }         format.xml { render :xml => @product.errors, :status => :unprocessable_entity }       end     end   end

  # DELETE /products/1   # DELETE /products/1.xml   def destroy     @product = Product.find(params[:id])     @product.destroy

    respond_to do |format|       format.html { redirect_to(products_url) }       format.xml { head :ok }     end   end end

Regards

Hi, stack level too deep error is not due to code modifications. Taking a look at following article might solve your problem 'Ruby: stack level too deep (SystemStackError) | Dalibor Nasevic’.

The error is due to system stack level being less then required by application.

Thanks, Anubhaw

Anubhaw Prakash wrote:

Hi, stack level too deep error is not due to code modifications. Taking a look at following article might solve your problem 'Ruby: stack level too deep (SystemStackError) | Dalibor Nasevic’.

The error is due to system stack level being less then required by application.

Thanks, Anubhaw

I went to the link abov and followed the steps and still getting the same SYSTEM STACK ERROR. I wonder what could be causing this issue, Im running rails 2.3.8.

Thanks

You don't need to reference the show.html.erb in your controller... template implementations have that "# show.html.erb" text as a reminder of which view is invoked.

def show   @product = Product.find(params[:id])   respond_to do |format|     format.html # show.html.erb     format.xml { render :xml => @product }   end end

should be fine.

In your view, are you referencing a method on the model that may be recursing indefinitely? I've seen instances of tht which will cause a stack level too deep error... but yours seems to be related to the find operation in the controller.

Have you migrated your Products table with fields? - I don't know if a column-less table would have this effect..., never tried it myself.

Ar Chron wrote:

You don't need to reference the show.html.erb in your controller... template implementations have that "# show.html.erb" text as a reminder of which view is invoked.

def show   @product = Product.find(params[:id])   respond_to do |format|     format.html # show.html.erb     format.xml { render :xml => @product }   end end

should be fine.

In your view, are you referencing a method on the model that may be recursing indefinitely? I've seen instances of tht which will cause a stack level too deep error... but yours seems to be related to the find operation in the controller.

Have you migrated your Products table with fields? - I don't know if a column-less table would have this effect..., never tried it myself.

Thanks Chron, that solution resolved my issue, I dropped the reference to show.html and it worked perfect. SO if the method is the same name as the view, I should never reference that view in the controller inside the method definition?

regards

It's unnecessary but harmless. The problem here is that you wrote

render show.html.erb

rather than render 'show' or something like that - with the above rails thinks it needs to call the show method (and then call a method named html on that and so) so you end up recursing into your own action infinitely

Fred