diff options
author | Mark Thomson <nzl216@gmail.com> | 2012-03-17 22:29:46 -0500 |
---|---|---|
committer | Mark Thomson <nzl216@gmail.com> | 2012-03-17 22:29:46 -0500 |
commit | f2bc404ba82431d32a35b4de15cb21f179bc24c7 (patch) | |
tree | ca58ce1118eeda244ced0ef0a1d94ed7ca38e5e0 /guides/code/getting_started/app/controllers/posts_controller.rb | |
parent | 98b4ef730696062b624c508d22ca76d9caa018cc (diff) | |
parent | 6ce54d4ba8c220a84e55e7dd798d364c3f48d9f7 (diff) | |
download | rails-f2bc404ba82431d32a35b4de15cb21f179bc24c7.tar.gz rails-f2bc404ba82431d32a35b4de15cb21f179bc24c7.tar.bz2 rails-f2bc404ba82431d32a35b4de15cb21f179bc24c7.zip |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'guides/code/getting_started/app/controllers/posts_controller.rb')
-rw-r--r-- | guides/code/getting_started/app/controllers/posts_controller.rb | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/guides/code/getting_started/app/controllers/posts_controller.rb b/guides/code/getting_started/app/controllers/posts_controller.rb new file mode 100644 index 0000000000..1581d4eb16 --- /dev/null +++ b/guides/code/getting_started/app/controllers/posts_controller.rb @@ -0,0 +1,84 @@ +class PostsController < ApplicationController + http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index + # GET /posts + # GET /posts.json + def index + @posts = Post.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @posts } + end + end + + # GET /posts/1 + # GET /posts/1.json + def show + @post = Post.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @post } + end + end + + # GET /posts/new + # GET /posts/new.json + def new + @post = Post.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @post } + end + end + + # GET /posts/1/edit + def edit + @post = Post.find(params[:id]) + end + + # POST /posts + # POST /posts.json + def create + @post = Post.new(params[:post]) + + respond_to do |format| + if @post.save + format.html { redirect_to @post, notice: 'Post was successfully created.' } + format.json { render json: @post, status: :created, location: @post } + else + format.html { render action: "new" } + format.json { render json: @post.errors, status: :unprocessable_entity } + end + end + end + + # PUT /posts/1 + # PUT /posts/1.json + def update + @post = Post.find(params[:id]) + + respond_to do |format| + if @post.update_attributes(params[:post]) + format.html { redirect_to @post, notice: 'Post was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @post.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /posts/1 + # DELETE /posts/1.json + def destroy + @post = Post.find(params[:id]) + @post.destroy + + respond_to do |format| + format.html { redirect_to posts_url } + format.json { head :no_content } + end + end +end |