Forum: Ruby on Rails Create and then show

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.
37ae59664e4d6c45b5ec0d8a290133f1?d=identicon&s=25 Matthew Routley (Guest)
on 2006-02-17 01:33
(Received via mailing list)
I am attempting to redirect to the show method after creating a new
object. So, for example, once a new post is created the user is
presented with the new post in a show view. The problem is I can't get
the correct <at> post.id to send to the show method. Rails always
returns 0 for this value.

Here's what I have so far:

class ContentController < ApplicationController
  def new
    <at> post = Post.new(params[:post])
    if request.post? and <at> post.save
      redirect_to :action => 'show', :id => <at> post.id
    else
       ...
    end
  end

  def show
    <at> post = Post.find(params[:id])
  end

  ...

end

According to the log, this is what is being called:

Processing ContentController#show (for ...) [GET]
Parameters: {"action"=>"show", "id"=>"0", "controller"=>"content"}

which clearly doesn't exist. However, the post_id column in the
database is being updated correctly.

I've examined the source code for Typo and this appears to be exactly
what they are doing -- except of course it works for Typo. Any
suggestions?

Running: sqlite3, Rails 1.0.0, on Mac OS X 10.4.5

Thanks,
Matt
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2006-02-17 06:45
(Received via mailing list)
Can you show us the sql statement you used to create posts table?

--
Kent
http://www.datanoise.com
37ae59664e4d6c45b5ec0d8a290133f1?d=identicon&s=25 Matthew Routley (Guest)
on 2006-02-17 18:06
(Received via mailing list)
Kent,

> Can you show us the sql statement you used to create posts table?

Certainly, the posts table was generated with:

CREATE TABLE posts (
  'id' INTEGER PRIMARY KEY NOT NULL,
  'content' varchar(40),
  'more_content varchar(40),
  'category_id' integer,
  'created_at' datetime,
  'updated_at' datetime,
  'version' integer
);

I am also using the acts_as_versioned plugin, and the login and user
engines.

Cheers,
Matt
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2006-02-17 18:33
(Received via mailing list)
Don't you want to add autoincrement to 'id' field?

Kent
---
http://www.datanoise.com
37ae59664e4d6c45b5ec0d8a290133f1?d=identicon&s=25 Matthew Routley (Guest)
on 2006-02-17 19:22
(Received via mailing list)
Kent,

> Don't you want to add autoincrement to 'id' field?

No, I don't believe so. My understanding is that sqlite3 doesn't require
(or
support?) an autoincrement field.

http://www.sqlite.org/faq.html#q1

Good suggestion though, for a moment I thought I'd made a rather obvious
mistake.

Matt
3ccecc71b9fb0a3d7f00a0bef6f0a63a?d=identicon&s=25 Kent Sibilev (Guest)
on 2006-02-17 19:28
(Received via mailing list)
Sure it doesn't require, but Rails needs some support from the
database in order to generate unique ids.

Kent
---
http://www.datanoise.com
37ae59664e4d6c45b5ec0d8a290133f1?d=identicon&s=25 Matthew Routley (Guest)
on 2006-02-17 20:35
(Received via mailing list)
Kent,

> Sure it doesn't require, but Rails needs some support from the
> database in order to generate unique ids.

Fair enough, I've changed the schema to:

CREATE TABLE posts (
  'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  'content' varchar(40),
  'more_content' varchar(40),
  'category_id' integer,
  'created_at' datetime,
  'updated_at' datetime,
  'version' integer
);

but the problem persists. So, I hooked up the Typo code which uses the
same
method as my Content#new and doesn't include the AUTOINCREMENT
declaration in
the sqlite schema. Running Typo locally produces the same id=0 error.
Presumably
there is something wrong with my local setup.

Now my question has changed. Any suggestions on using sqlite3 with Rails
development? I'm using sqlite3-ruby (1.1.0), sqlite 3.1.3, and
activerecord
(1.13.2) with a fresh (~4 days) installation of Mac OS X 10.4 and
compiled rails
tools following
http://hivelogic.com/articles/2005/12/01/ruby_rail...

Cheers,
Matt
37ae59664e4d6c45b5ec0d8a290133f1?d=identicon&s=25 Matthew Routley (Guest)
on 2006-02-17 20:56
(Received via mailing list)
Thanks to some input from Kent the problem was isolated to my local
setup.
Advice from http://wiki.rubyonrails.org/rails/pages/HowtoUseSQLite
revealed that
I needed swig so:

sudo port install swig
sudo gem install sqlite3-ruby

fixed the id=0 problem with sqlite3.

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