1 dr alexiei dingli web science stream a ror blog
TRANSCRIPT
1
Dr Alexiei Dingli
Web Science Stream
A ROR Blog
2
Create a Blog in 15 minutes ...
Let’s get our hands dirty!
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)
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
So far so good ...
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
What’s in a relation?
8
has_many :posts
Add to blog\app\models\category.rb
9
belongs_to :category
Add to blog\app\models\post.rb
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
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
12
def edit
@post = Post.find(params[:id])
@cat = Category.find(:all)
end
blog\app\controllers\post_controller.rb
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
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
15
Replace
<p>
<%= f.label :category_id %>
<br />
<%= f.text_field :category_id %>
</p>
With
blog\app\views\posts\new.html.erb
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
17
Replace
<p>
<%= f.label :category_id %>
<br />
<%= f.text_field :category_id %>
</p>
With
blog\app\views\posts\edit.html.erb
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
19
Replace
<p>
<b>Category:</b>
<%=h @post.category_id %>
</p>
With
blog\app\views\posts\show.html.erb
20
<p>
<b>Category:</b>
<%=h @post.category.category_name %>
</p>
blog\app\views\posts\show.html.erb
21
Replace
<td><%=h post.category_id %></td>
With
blog\app\views\posts\index.html.erb
22
<td><%=h post.category.category_name %></td>
blog\app\views\posts\index.html.erb
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 ...
24
Questions?