Create and then show


#1

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


#2

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


Kent
http://www.datanoise.com


#3

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


#4

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

Kent

http://www.datanoise.com


#5

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

Kent

http://www.datanoise.com


#6

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_rails_lighttpd_mysql_tiger

Cheers,
Matt


#7

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


#8

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