From 860efecd8f44e112e447f4a233115090bdd8c3db Mon Sep 17 00:00:00 2001 From: James Miller Date: Sun, 21 Sep 2008 08:32:34 -0700 Subject: New and create actions --- .../getting_started_with_rails.txt | 32 +++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'railties/doc/guides/getting_started_with_rails') 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 -- cgit v1.2.3