diff options
author | James Miller <james@jkmillertech.com> | 2008-09-21 08:32:34 -0700 |
---|---|---|
committer | James Miller <james@jkmillertech.com> | 2008-09-21 08:32:34 -0700 |
commit | 860efecd8f44e112e447f4a233115090bdd8c3db (patch) | |
tree | 2a6224a27eda4b2553bc8cdc2ea9b3009767c20c | |
parent | d9b67223e07e91ceed9da1f4f7d23aa6ef5ed27a (diff) | |
download | rails-860efecd8f44e112e447f4a233115090bdd8c3db.tar.gz rails-860efecd8f44e112e447f4a233115090bdd8c3db.tar.bz2 rails-860efecd8f44e112e447f4a233115090bdd8c3db.zip |
New and create actions
-rw-r--r-- | railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt b/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt index 2805e5629d..c181d1432c 100644 --- a/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt +++ b/railties/doc/guides/getting_started_with_rails/getting_started_with_rails.txt @@ -317,7 +317,37 @@ This time, we're setting `@post` to a single record in the database that is sear ==== New & Create -Description of new and create actions +In your controller, you'll see the `new` and `create` actions, which are used together to create a new record. Our `new` action simply instantiates a new Post object without any parameters: + +----------------------------------------- +def new + @post = Post.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @post } + end +end +---------------------------------------- + +Our `create` action, on the other hand, instantiates a new Post object while setting its attributes to the parameters that we specify in our form: + +---------------------------------------- +def create + @post = Post.new(params[:post]) + + respond_to do |format| + if @post.save + flash[:notice] = 'Post was successfully created.' + format.html { redirect_to(@post) } + format.xml { render :xml => @post, :status => :created, :location => @post } + else + format.html { render :action => "new" } + format.xml { render :xml => @post.errors, :status => :unprocessable_entity } + end + end +end +--------------------------------------- ==== Edit & Update |