1 / 37

Creating a wiki blog

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 or one of the apps that come with the distribution Run ruby script/server Go to localhost:3000/…. What is a wiki blog?.

zuri
Download Presentation

Creating a wiki blog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Creating a wiki blog

  2. 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/….

  3. 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?

  4. 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.

  5. The tables in blog_development

  6. posts table

  7. wiki_page table

  8. 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.

  9. the wiki blog

  10. Directory structure is pretty big compared to other projects

  11. Listing all posts to the blog

  12. Listing the pages (shows slug)

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

  14. After adding a new wiki page

  15. A new posting

  16. The posting has links to slugs defined elsewhere

  17. 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)

  18. 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

  19. Work from the blog folder • After you create the blog application, switch to its folder to continue work directly in that application: cd blog

  20. config/database.yml for MySQL development: 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

  21. 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

  22. 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

  23. 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

  24. 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.

  25. 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.

  26. 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.

  27. Your db/migrate/create_posts.rb will look a little like this class CreatePosts <ActiveRecord::Migration defself.up create_table :posts do |t| t.string :name t.string :title t.text :content t.timestamps end end defself.down drop_table :posts End end

  28. 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.

  29. wikifying • Add the following lines to the wikipage model: validates_uniqueness_of :slug wiki_column :body • And to the Post model: wiki_column :content

  30. 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>

  31. Do the same in post Fix app/views/posts/show.rhtml so it has code like <p> <b>Content:</b> <%= @post.wiki_content %> </p>

  32. in routes.rb file • add a new resource to the routes.rb file map.resources :wiki, :controller => 'wiki_pages'

  33. 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.

  34. 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

  35. 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:

  36. 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 %>

  37. You can use a filter & a function to reduce redundancy class PostsController < ApplicationController before_filter :find_post, :only => [:show, :edit, :update, :destroy] # ...def show # ...enddef edit enddef update # ...enddef destroy # ...end private def find_post @post = Post.find(params[:id]) end end

More Related