# Create and then show

**URL:** <https://www.ruby-forum.com/t/create-and-then-show/53925>\
**Category:** Rails\
**Created:** [February 17, 2006, 1:33am UTC](https://www.ruby-forum.com/t/create-and-then-show/53925 "2006-02-17T01:33:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 1:33am UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/1 "2006-02-17T01:33:30Z")

</div>

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 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  
 post = Post.new(params[:post])  
if request.post? and post.save  
redirect\_to :action =\> ‘show’, :id =\> post.id  
else  
…  
end  
end

def show  
 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

---

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 6:45am UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/2 "2006-02-17T06:45:38Z")

</div>

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

–  
Kent

> **[DATANOISE.COM](https://www.datanoise.com/)**
>
> DATANOISE.COM - Contact us for any business inquiries

---

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/3 "2006-02-17T18:06:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 6:33pm UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/4 "2006-02-17T18:33:40Z")

</div>

Don’t you want to add autoincrement to ‘id’ field?

## Kent

> **[DATANOISE.COM](https://www.datanoise.com/)**
>
> DATANOISE.COM - Contact us for any business inquiries

---

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/5 "2006-02-17T19:28:29Z")

</div>

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

## Kent

> **[DATANOISE.COM](https://www.datanoise.com/)**
>
> DATANOISE.COM - Contact us for any business inquiries

---

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/6 "2006-02-17T20:35:22Z")

</div>

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

> **[Dan Benjamin](https://danbenjamin.com)**
>
> Dan Benjamin's personal website.

Cheers,  
Matt

---

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/7 "2006-02-17T20:56:43Z")

</div>

Thanks to some input from Kent the problem was isolated to my local  
setup.  
Advice from [http://wiki.rubyonrails.org/rails/pages/HowtoUseSQLite](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

---

<div class="post-metadata">

**Author:** ![Matthew\_Routley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/da6949/32.png) [@Matthew\_Routley](https://www.ruby-forum.com/u/Matthew_Routley)\
**Post date:** [February 17, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/create-and-then-show/53925/8 "2006-02-17T19:22:23Z")

</div>

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](http://www.sqlite.org/faq.html#q1)

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

Matt
