Top Banner
1 Dr Alexiei Dingli Web Science Stream A ROR Twitter
25

1 Dr Alexiei Dingli Web Science Stream A ROR Twitter.

Jan 04, 2016

Download

Documents

LESLIE Short
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: 1 Dr Alexiei Dingli Web Science Stream A ROR Twitter.

1

Dr Alexiei Dingli

Web Science Stream

A ROR Twitter

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

2

Create Twitter in 30 minutes ...

Let’s get our hands dirty!

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

3

What is Twitter?

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

4

• Write a pragraph describing what you want to achieve ...

My web app should work in a similar way to twitter. Users should be able to register with the site and create short posts. Users should be able to follow other users. Each user should be able to see their own posts plus the users they are following.

Step 1

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

5

• Creating the application

> rails twitter

Step 2

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

6

• Let’s create the basic database post model

> ruby script/generate model post message:text  

• Setup the directories

> rake db:migrate  

Step 3

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

7

> ruby script/generate controller posts 

Step 4Whose in control? The controller

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

8

The Posts controller

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

9

• Create a partial– app\views\posts\_post.html.erb

<p><b>Posted <%= time_ago_in_words(post.created_at) %> ago</b></p>  

<p><%= post.message %></p> 

Step 5How shall we look at them? Through views

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

10

• Create a view– app\views\posts\index.html.erb

<%= render :partial => @posts %>

Step 6How shall we look at them? Through views

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

11

• Create a partial– app\views\posts\_message_form.html.erb

Step 7How shall we enter tweets?

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

12

• Modify• app\views\posts\index.html.erb

• And include at the top ...

<%= render :partial => "message_form" %>

Step 8Putting everything together!

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

13

• Modify• config\routes.rb

• And include at the bottom ...

Step 9Where can I find it? Look at the routes!

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

14

• Try it out ...–  ruby script/server  

Step 10

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

15

• Modify the create method to accept java script files

def create  

  @post = Post.create(:message => params[:message])  

  respond_to do |format|  

    if @post.save  

      format.html { redirect_to posts_path }  

      format.js  

    else  

      flash[:notice] = "Message failed to save."  

      format.html { redirect_to posts_path }  

    end  

  end  

end  

Spicing it up with AJAX (1)

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

16

• Modify• app\views\layouts\posts.html.erb

Spicing it up with AJAX (2)

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

17

Use the magic div tag in the index.html.erb

<%= render :partial => "message_form" %>  

<div id="posts">

  <%= render :partial => @posts %>  

</div>  

Spicing it up with AJAX (3)

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

18

Use the magic div tag in the _post.html.erb

<% div_for post do %>  

   <p><b>Posted <%= time_ago_in_words(post.created_at) %> ago</b></p>  

   <p><%= post.message %></p>  

<% end %>  

Spicing it up with AJAX (4)

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

19

Modify the form tag in the partial _message_form.html.erb

<% form_remote_tag

:url => {:controller => "posts", :action => "create"} do %>

Spicing it up with AJAX (5)

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

20

The magic of Ruby Java Script. Create a views/posts/create.rjs

page.insert_html :top, :posts, :partial => @post  

page[@post].visual_effect :highlight 

Spicing it up with AJAX (6)

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

21

How’s the spice?

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

22

Create in public/stylesheets a file called layout.css and paste the following layout ...

Ugly duckling? Use some CSS

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

23

• In the header of the app/views/layouts/posts.html.erb add

<%= stylesheet_link_tag 'layout' %>

Activate the CSS

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

24

Voila!

Page 25: 1 Dr Alexiei Dingli Web Science Stream A ROR Twitter.

25

Questions?