To Examples

MiniTwitter Example

Directions

  1. Create a new Rails project named MiniTwitter.
  2. Generate a Rails scaffold named User with the field screen_name (datatype string).
  3. Generate a Rails scaffold named Follower with the field screen_name (datatype string).
  4. Generate a Rails scaffold named Connection with fields follower_id (datatype integer) and user_id (datatype integer).
  5. Generate a Rails scaffold named Comment with fields content (datatype text) and user_id (datatype integer).
  6. Source code for the seed file MiniTwitter/db/seed.rb.
  7. Run these Rails commands in a Command Prompt or Terminal Window:
    > rake db:migrate
    > rake db:seed
    > rails s
    
  8. View the index pages of the controllers you created with these URLs:
    http://localhost:3000/users
    http://localhost:3000/followers
    http://localhost:3000/connections
    http://localhost:3000/comments
    
  9. To make the Connection index more readable, in the file MiniTwitter/app/views/connections/index.html.erb, change the line
    <%= connection.follower_id %>
    
    to
    <%= connection.follower.screen_name %>
    
    Also change the line
    <%= connection.user_id %>
    
    to
    <%= connection.user.screen_name %>
    
  10. Add these Ruby statements to the new method and the edit method of the Connection controller in the file MiniTwitter/app/controllers/connection_controller.rb:
     @follower_options = Follower.order(:screen_name).all.collect do |f|
      [f.screen_name, f.id]
    end
    
    @user_options = User.order(:screen_name).all.collect do |u|
      [u.screen_name, u.id]
    end
    
  11. In the Connection input form in the file MiniTwitter/app/views/connections/_form.html.erb, replace the line
    <%= f.number_field :follower_id %>
    
    with the line
    <%= f.delect :follower_id, @follower_options %>
    
  12. In the same Connection input form as in Step 10, replace the line
    <%= f.number_field :user_id %>
    
    with the line
    <%= f.delect :user_id, @user_options %>
    
  13. In the Connection index page, use the dropdown menus to add more connections between followers and users.
  14. At the bottom of the Follower show page, show all of the comments of all users that the follower of that view is following, sorted in order of most recent to least recent. Do this by adding the following source code in the file MiniTwitter/app/views/followers/show.html.erb:
    <% Comment.order(created_at: 'desc').all.each do |comm| %>
      <% followers = comm.user.connections.collect do |con| %>
        <% con.follower %>
      <% end %>
      <% if followers.include? @follower %> 
        <p>
          <%= comm.user.screen_name %> 
          <%= comm.created_at.strftime("%-m/%-d/%y %l:%M:%S%P") %><br />
          <%= comm.content %>
        </p>
      <% end %>
    <% end %>