1 dr alexiei dingli web science stream a ror blog

24
1 Dr Alexiei Dingli Web Science Stream A ROR Blog

Upload: brook-james

Post on 03-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

1

Dr Alexiei Dingli

Web Science Stream

A ROR Blog

Page 2: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

2

Create a Blog in 15 minutes ...

Let’s get our hands dirty!

Page 3: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

3

1. Open a ruby consoleInstant 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)

Page 4: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

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)

Page 5: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

5

So far so good ...

Page 6: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

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)

Page 7: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

7

What’s in a relation?

Page 8: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

8

has_many :posts

Add to blog\app\models\category.rb

Page 9: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

9

belongs_to :category

Add to blog\app\models\post.rb

Page 10: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

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

Page 11: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

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

end

blog\app\controllers\post_controller.rb

Page 12: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

12

def edit

@post = Post.find(params[:id])

@cat = Category.find(:all)

end

blog\app\controllers\post_controller.rb

Page 13: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

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

end

end

blog\app\controllers\post_controller.rb

Page 14: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

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

end

blog\app\controllers\post_controller.rb

Page 15: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

15

Replace

<p>

<%= f.label :category_id %>

<br />

<%= f.text_field :category_id %>

</p>

With

blog\app\views\posts\new.html.erb

Page 16: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

16

<p>

<label for="category">Category<br/></label>

<select name="category">

<% @cat.each do |category| %>

<option value="<%= category.id %>">

<%= category.category_name %>

</option>

<% end %>

</select>

</p>

blog\app\views\posts\new.html.erb

Page 17: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

17

Replace

<p>

<%= f.label :category_id %>

<br />

<%= f.text_field :category_id %>

</p>

With

blog\app\views\posts\edit.html.erb

Page 18: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

18

<p>

<label for="category">Category<br/></label>

<select name="category">

<% @cat.each do |category| %>

<% if @post.category_id == category.id%>

<option value="<%= category.id %>" selected> <%= category.category_name %>

</option>

<% else %>

<option value="<%= category.id %>">

<%= category.category_name %>

</option>

<% end %>

<% end %>

</select>

</p>

blog\app\views\posts\edit.html.erb

Page 19: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

19

Replace

<p>

<b>Category:</b>

<%=h @post.category_id %>

</p>

With

blog\app\views\posts\show.html.erb

Page 20: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

20

<p>

<b>Category:</b>

<%=h @post.category.category_name %>

</p>

blog\app\views\posts\show.html.erb

Page 21: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

21

Replace

<td><%=h post.category_id %></td>

With

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

Page 22: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

22

<td><%=h post.category.category_name %></td>

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

Page 23: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

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/posts

That’s all, let’s try it ...

Page 24: 1 Dr Alexiei Dingli Web Science Stream A ROR Blog

24

Questions?