Commit 34603844 authored by Ali Yazdani's avatar Ali Yazdani

added associated routes

parent c3d19d74
GEM
remote: https://rubygems.org/
specs:
activemodel (4.2.4)
activesupport (= 4.2.4)
builder (~> 3.1)
activerecord (4.2.4)
activemodel (= 4.2.4)
activesupport (= 4.2.4)
arel (~> 6.0)
activesupport (4.2.4)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
backports (3.6.6)
bond (0.5.1)
builder (3.2.2)
coderay (1.1.0)
i18n (0.7.0)
json (1.8.3)
method_source (0.8.2)
minitest (5.8.0)
multi_json (1.11.2)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
puma (2.13.4)
rack (1.6.4)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
rake (10.4.2)
ripl (0.7.1)
bond (~> 0.5.1)
ripl-multi_line (0.3.1)
ripl (>= 0.3.6)
ripl-rack (0.2.1)
rack (>= 1.0)
rack-test (~> 0.6.2)
ripl (>= 0.7.0)
shotgun (0.9.1)
rack (>= 1.0)
sinatra (1.4.6)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sinatra-activerecord (2.0.7)
activerecord (>= 3.2)
sinatra (~> 1.0)
sinatra-contrib (1.4.6)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (>= 1.3, < 3)
slop (3.6.0)
sqlite3 (1.3.10)
thread_safe (0.3.5)
tilt (2.0.1)
tux (0.3.0)
ripl (>= 0.3.5)
ripl-multi_line (>= 0.2.4)
ripl-rack (>= 0.2.0)
sinatra (>= 1.2.1)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
activesupport
pry
puma
rake
shotgun
sinatra
sinatra-activerecord
sinatra-contrib
sqlite3
tux
BUNDLED WITH
1.10.5
...@@ -2,3 +2,44 @@ ...@@ -2,3 +2,44 @@
get '/' do get '/' do
erb :index erb :index
end end
get '/users/new' do
erb :'users/new'
end
post '/users/new' do
name = params[:name]
age = params[:age]
city = params[:city]
user = User.new(name: name, age: age, city: city)
if user.save
redirect "/users/#{user.id}"
else
erb :'users/new'
end
end
get '/users/:id' do
@user = User.find(params[:id])
erb :'users/show'
end
get '/users/:id/tweets/new' do
@user = User.find(params[:id])
erb :'tweets/new'
end
post '/users/:id/tweets/new' do
content = params[:content]
@user = User.find(params[:id])
tweet = @user.tweets.new(content: content)
if tweet.save
redirect "/users/#{@user.id}"
else
redirect "/users/#{@user.id}/tweets/new"
end
end
class Tweet < ActiveRecord::Base
belongs_to :user
end
\ No newline at end of file
class User < ActiveRecord::Base
has_many :tweets
end
\ No newline at end of file
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/javascript/application.js"></script> <script src="/javascript/application.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<title></title> <title></title>
</head> </head>
<body> <body>
......
<h1><%= @user.name %>, please create a new tweet</h1>
<form method="POST" action="/users/<%= @user.id %>/tweets/new">
<label for="content">Tweet Content:</label>
<textarea id="content" name="content"></textarea>
<input type="submit" value="create new tweet" />
</form>
\ No newline at end of file
<h1>Create a new user</h1>
<form method="POST" action="/users/new" >
<label for="name">Name:</label>
<input type="text" id="name" name="name" /><br/>
<label for="age">Age</label>
<input type="number" id="age" name="age" /><br/>
<label for="city">City:</label>
<input type="text" id="city" name="city" /><br/>
<input type="submit" value="create new user" />
</form>
\ No newline at end of file
<h1>Welcome to <%= @user.name %>'s tweets page</h1>
<a href="/users/<%= @user.id %>/tweets/new">Create a new tweet</a>
<h2>Here is a list of all my tweets</h2>
<table class="table-striped">
<th>content</th>
<% @user.tweets.reverse.each do |tweet| %>
<tr>
<td><%= tweet.content %></td>
</tr>
<% end %>
</table>
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.integer :age
t.string :city
t.timestamps
end
end
end
\ No newline at end of file
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.text :content
t.string :img_url
t.references :user
t.timestamps
end
end
end
\ No newline at end of file
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead # This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to # of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition. # incrementally modify your database, and then regenerate this schema definition.
...@@ -10,11 +11,22 @@ ...@@ -10,11 +11,22 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140321144528) do ActiveRecord::Schema.define(version: 2) do
create_table "users", force: true do |t| create_table "tweets", force: :cascade do |t|
t.text "content"
t.string "img_url"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", force: :cascade do |t|
t.string "name" t.string "name"
t.string "email" t.integer "age"
t.string "city"
t.datetime "created_at"
t.datetime "updated_at"
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment