Permalink
Browse files

Added simple form example

  • Loading branch information...
1 parent e7868d9 commit a846c93e3cf6d279a9710c7e840a8f1f6b0a3787 @robdimarco committed Jun 12, 2012
@@ -0,0 +1,18 @@
+class BlogPostsController < ApplicationController
+ def new
+ @blog_post = BlogPost.new
+ end
+ def create
+ @blog_post = BlogPost.new params[:blog_post]
+ if @blog_post.save
+ flash[:success] = "Blog post saved"
+ redirect_to edit_blog_post_path(@blog_post)
+ else
+ render "new"
+ end
+ end
+ def edit
+ @blog_post = BlogPost.find(params[:id])
+ end
+
+end
View
@@ -0,0 +1,8 @@
+# Created with
+# rails g model BlogPost title:string body:text url:string email:string post_date:datetime
+class BlogPost < ActiveRecord::Base
+ validates :body, presence: true
+ validates :title, presence: true
+ validates :url, format:{with: /https?:\/\/[a-z\.]+\//, message: "Must be like http://www.google.com/"}
+ attr_accessible :body, :email, :post_date, :title, :url
+end
@@ -9,7 +9,7 @@
.nav-collapse
%ul.nav
%li.active
- %a{href: "#"} Home
+ %a{href: "/"} Home
%li
%a{href: "#"} Link
%li
@@ -0,0 +1,7 @@
+= simple_form_for(@blog_post, :html => {:class => 'form-horizontal' }) do |form|
+ = form.input :title
+ = form.input :body
+ = form.input :email
+ = form.input :url, label: "URL"
+ = form.input :post_date
+ = form.submit 'Save'
@@ -0,0 +1,3 @@
+.page-header
+ %h1 Edit #{@blog_post.title}
+= render "form"
@@ -0,0 +1,3 @@
+.page-header
+ %h1 Create Blog Post
+= render "form"
@@ -1,4 +1,3 @@
-= render "navbar"
.container
.row
.span3
@@ -6,5 +6,6 @@
= csrf_meta_tags
= stylesheet_link_tag "application", :media => "all"
%body{data:{spy:"scroll"}}
+ = render "navbar"
= yield
= javascript_include_tag "application"
View
@@ -48,7 +48,8 @@
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
- root :to => 'home#index'
+ resources :blog_posts
+ root :to => 'blog_posts#new'
# See how all your routes lay out with "rake routes"
@@ -0,0 +1,13 @@
+class CreateBlogPosts < ActiveRecord::Migration
+ def change
+ create_table :blog_posts do |t|
+ t.string :title
+ t.text :body
+ t.string :url
+ t.string :email
+ t.datetime :post_date
+
+ t.timestamps
+ end
+ end
+end
View
@@ -0,0 +1,26 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120612042915) do
+
+ create_table "blog_posts", :force => true do |t|
+ t.string "title"
+ t.text "body"
+ t.string "url"
+ t.string "email"
+ t.datetime "post_date"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+end

0 comments on commit a846c93

Please sign in to comment.