Forum: Ruby on Rails Cookies are not saved

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
143cca9f26d2421816f3758638b74647?d=identicon&s=25 Adam Meyer (outsida)
on 2008-11-17 21:54
Hi,

I'm developing my first RoR application and working first time with
cookies on RoR.

I create a product object, save it and set the id into the cookie

  def create
    @image = Image.new(params[:image])

    respond_to do |format|
      if @image.save
        @product = Product.new
        @product.image_id = @image.id
        @product.order_id = @order.id
        @product.save
        cookies[:product_id] = @product.id.to_s
        flash[:notice] = 'Image was successfully created.'
        format.html {redirect_to('/karte/waehlen')}
        format.xml  { render :xml => @image, :status => :created,
:location => @image }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @image.errors, :status =>
:unprocessable_entity }
      end
    end
  end

Then I redirect to the "karte" controller and "waehlen" action

The karte controller looks like this

class KarteController < ApplicationController
    def waehlen
      @product = Product.find(cookies[:product_id])
      @image = Image.find(@product.image_id)
    end
end

I try to get the product_id from the cookie.

The waehlen.html.erb looks like

<div id="formatCenterContainer">
  <%= image_tag(@image.filename, :size => "190x153") %>
</div>

I get a NoMethodError

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.filename

Extracted source (around line #12):

9:
10: </div>
11: <div id="formatCenterContainer">
12:   <%= image_tag(@image.filename, :size => "190x153") %>


I checked the Cookie folder on my filesystem. There is no Cookie.

Do I have activate cookies? I already browsed for the problem, but I
find only information about read/write cookies and other options.

Cheers Adam
This topic is locked and can not be replied to.