Top Banner
Creating a wiki blog
37

Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Creating a wiki blog

Page 2: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Run apps that come with instant rails distribution

• select I/rails applications/open ruby console window

• Cd to cookbook or typo or one of the apps that come with the distribution

• Run ruby script/server• Go to localhost:3000/….

Page 3: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

What is a wiki blog?

• What is a wiki? A wiki is a page or collection of Web pages designed to enable anyone who accesses it to contribute or modify content, using a simplified markup .

• What is a blog? A blog (a contraction of the term weblog) is a type of website, usually maintained by an individual with regular entries of commentary, descriptions of events, or other material such as graphics or video. Entries are commonly displayed in reverse-chronological order. "Blog" can also be used as a verb, meaning to maintain or add content to a blog.

• How about combining these?

Page 4: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

elements

• We will need a mysql table with information on wiki pages and postings.

• We will need to store names, dates created and dates updated for postings and pages.

• We will need a (wiki) column for slugs, since these can be referenced by other entries.

Page 5: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

The tables in blog_development

Page 6: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

posts table

Page 7: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

wiki_page table

Page 8: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

About the database

• As you’ll see, rake files in the db directory can be created to build the database tables.

• You could also create the database using phpmyadmin.

Page 9: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

the wiki blog

Page 10: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Directory structure is pretty big compared to other projects

Page 11: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Listing all posts to the blog

Page 12: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Listing the pages (shows slug)

Page 13: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Adding a wiki page: use [] to link to a slug defined elsewhere

Page 14: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

After adding a new wiki page

Page 15: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

A new posting

Page 16: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

The posting has links to slugs defined elsewhere

Page 17: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Create your rails app

• Remember to use the instant rails icon to start servers, and to select I/rails applications/open ruby console window in order to run these “dos” commands!

• In Rails_Apps:rails blog • This will create a Rails application that uses a SQLite

database for data storage. If you prefer to use MySQL, run this command instead:

rails blog -d mysql • (You may not need to do the mysql part, and you’ll need

to check your database yml file in any case)

Page 18: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

I used google wiki column plug-in for ruby which requires red cloth gem

• Wiki column link information is at http://code.google.com/p/wiki-column• You need to install redcloth. Their site is at http://redcloth.org/• To get Red Cloth gem, in DOS use

ruby gem install Red Cloth• The plugin wiki_column…in your blog DOS window:

ruby script/plugin install http://wiki-column.googlecode.com/svn/trunk/wiki_column

Page 19: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Work from the blog folder

• After you create the blog application, switch to its folder to continue work directly in that application:

cd blog

Page 20: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

config/database.yml for MySQLdevelopment: adapter: mysql database: blog_development username: root password: host: localhost

# Warning: The database defined as 'test' will be erased and# re-generated from your development database when you run 'rake'.# Do not set this db to the same as development or production.test: adapter: mysql database: blog_test username: root password: host: localhost

production: adapter: mysql database: blog_production username: root password: host: localhost

Page 21: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Create the db from RoR

To create the database, in DOS in your RoR directory:

rake db:create• Or you can go to the Instant Rails menu and

select configure/database using phpmyadmin and create the blog_development database that way. It must be called blog_development if your rails app is called blog!

• Next create controllers for home and index:ruby script/generate controller home index

Page 22: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Create a home controller

• blog>ruby script/generate controller home index

• The file app/views/home/index.html.erb contains one line:

<h1>Hello, Rails!</h1>

• You may change this to some other welcome message html if you like

Page 23: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Now run the server

ruby script/server

• will run the default server (webrick or mongrel) at localhost:3000.

• Navigate to http://localhost:3000/home/index

• This will show the default rails page. The set your own page delete public/index.html

Page 24: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Config routes• Now, you have to tell Rails where your actual home page is located.

Open the file config/routes.rb in your editor. This is your application's, routing file, which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. At the bottom of the file you'll see the default routes:

• map.connect ':controller/:action/:id'map.connect ':controller/:action/:id.:format' The default routes handle simple requests such as /home/index: Rails translates that into a call to the index action in the home controller. As another example, /posts/edit/1 would run the edit action in the posts controller with an id of 1.

• To hook up your home page, you need to add another line to the routing file, above the default routes:

• map.root :controller => "home" • Now navigating to localhost:3000 will show your home index view.

Page 25: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

scaffolding

• While scaffolding will get you up and running quickly, the "one size fits all" code that it generates is unlikely to be a perfect fit for your application. In most cases, you'll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch. In particular, code for show, edit, delete, new methods are generated.

Page 26: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Using scaffolding to generate two models

ruby script/generate scaffold WikiPage slug:string body:text created_at:datetime updated_at:datetime

ruby script/generate scaffold Post name:string title:string content:text created_at:datetime updated_at:datetime

• Don't forget to migrate.

rake db:migrate

This will generate db tables for WikiPage and Post

Note: We will “wikify” the content part of the post.

Page 27: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Your db/migrate/create_posts.rb will look a little like this

class CreatePosts <ActiveRecord::Migration def self.up create_table :posts do |t| t.string :name t.string :title t.text :content t.timestamps end end def self.down drop_table :posts Endend

Page 28: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Adding a Link

• To hook the posts up to the home page you've already created, you can add a link to the home page. Open /app/views/home/index.html.erb and modify it as follows:

• <h1>Hello, Rails!</h1>• <%= link_to "My Blog", posts_path %> • To create similar links on other index, new,

show, etc pages, cut and paste the links you want.

Page 29: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

wikifying

• Add the following lines to the wikipage model:

validates_uniqueness_of :slug

wiki_column :body

• And to the Post model:

wiki_column :content

Page 30: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

In WikiPage show.rhtml

• Change the line<%=h @wiki_page.body %>• to <%= @wiki_page.wiki_body %>• So app/views/wiki_page/show.rhtml has some

code like:<p> <b>Body:</b> <%= @wiki_page.wiki_body %></p>

Page 31: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Do the same in post

Fix app/views/posts/show.rhtml so it has code like

<p>

<b>Content:</b>

<%= @post.wiki_content %>

</p>

Page 32: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

in routes.rb file

• add a new resource to the routes.rb file

map.resources :wiki, :controller => 'wiki_pages'

Page 33: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Using slugs

• Define unique slug names for your wiki pages and provide body for them which consists of a URL.

• Refer to wiki pages in post content with [slugname]

• You can go to Textile and get more information on syntax.

Page 34: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Add validation to the Post

In app/models/post.rb:

class Post < ActiveRecord::Base validates_presence_of :name, :title validates_length_of :title, :minimum => 5

wiki_column :content

end

Page 35: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

Using Partials to Eliminate View Duplication

• As you saw earlier, the scaffold-generated views for the new and edit actions are largely identical. You can pull the shared code out into a partial template. This requires editing the new and edit views, and adding a new template. The new _form.html.erb template should be saved in the same app/views/posts folder as the files from which it is being extracted:

Page 36: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

New files & partial• new.html.erb:<h1>New post</h1><%= render :partial => "form" %><%= link_to 'Back', posts_path %> • edit.html.erb:<h1>Editing post</h1><%= render :partial => "form" %><%= link_to 'Show', @post %> |<

%= link_to 'Back', posts_path %>• _form.html.erb:<% form_for(@post) do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :title, "title" %><br /> <%= f.text_field :title %> </p> <p> <%= f.label :content %><br /> <%= f.text_area :content %> </p> <p> <%= f.submit "Save" %> </p><% end %>

Page 37: Creating a wiki blog. Run apps that come with instant rails distribution select I /rails applications/open ruby console window Cd to cookbook or typo.

You can use a filter & a function to reduce redundancy

class PostsController < ApplicationController

before_filter :find_post, :only => [:show, :edit, :update, :destroy]

# ... def show

# ... end def edit end def update # ... end def destroy # ... end private def find_post @post = Post.find(params[:id]) end

end