Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Dr Alexiei Dingli Web Science Stream A ROR Blog.

Similar presentations


Presentation on theme: "1 Dr Alexiei Dingli Web Science Stream A ROR Blog."— Presentation transcript:

1 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

2 2 Create a Blog in 15 minutes... Let’s get our hands dirty!

3 3 1.Open a ruby console Instant Rails Menu > Rails Applications > Open Ruby Console 2.Where you want to create your application type rails blog This will create the whole directory structure Steps (1)

4 4 3.Go into the todo dir cd blog 4.Load the server by typing ruby script/server 5. Access it through a normal browser by going to http://127.0.0.1:3000 Steps (2)

5 5 So far so good...

6 6 6. Let’s use some scaffolding... ruby script/generate scaffold category category_name:string ruby script/generate scaffold post post_title:string post_text:text date_of_creation:date category_id:integer 7. Let’s make the database rake db:migrate Steps (3)

7 7 What’s in a relation?

8 8 has_many :posts Add to blog\app\models\category.rb

9 9 belongs_to :category Add to blog\app\models\post.rb

10 10 def new : Function which retrieves data to be displayed, while creating a new post. def create : Function which accepts data from the form and stores it into the database when the form is submitted, while creating a new post. def edit : Function which retrieves data to be displayed, while editing a post. def update : Function which accepts data from the form and updates the database with new values when the form is submitted, while editing a post. Modifying controllers

11 11 def new @post = Post.new @cat = Category.find(:all) respond_to do |format| format.html # new.html.erb format.xml { render :xml => @post } end blog\app\controllers\post_contro ller.rb

12 12 def edit @post = Post.find(params[:id]) @cat = Category.find(:all) end blog\app\controllers\post_contro ller.rb

13 13 def create @post = Post.new(params[:post]) @post.category_id = (params[:category]) 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 blog\app\controllers\post_contro ller.rb

14 14 def update @post = Post.find(params[:id]) @post.category_id = (params[:category]) respond_to do |format| if @post.update_attributes(params[:post]) flash[:notice] = 'Post was successfully updated.' format.html { redirect_to(@post) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @post.errors, :status => :unprocessable_entity } end blog\app\controllers\post_contro ller.rb

15 15 Replace With blog\app\views\posts\new.html.erb

16 16 Category "> blog\app\views\posts\new.html.erb

17 17 Replace With blog\app\views\posts\edit.html.erb

18 18 Category " selected> "> blog\app\views\posts\edit.html.erb

19 19 Replace Category: With blog\app\views\posts\show.html.erb

20 20 Category: blog\app\views\posts\show.html.erb

21 21 Replace With blog\app\views\posts\index.html.erb

22 22 blog\app\views\posts\index.html.erb

23 23 Load the server by typing ruby script/server Access it through a normal browser by going to http://127.0.0.1:3000/categories http://127.0.0.1:3000/categories http://127.0.0.1:3000/posts That’s all, let’s try it...

24 24 Questions?


Download ppt "1 Dr Alexiei Dingli Web Science Stream A ROR Blog."

Similar presentations


Ads by Google