Added new ItemComment model
This commit is contained in:
parent
11d33e394b
commit
12045c684e
20
db/migrations/0002_add_item_comments_table.rb
Normal file
20
db/migrations/0002_add_item_comments_table.rb
Normal file
@ -0,0 +1,20 @@
|
||||
Sequel.migration do
|
||||
|
||||
up do
|
||||
create_table(:item_comments) do
|
||||
primary_key :id
|
||||
String :body, null: false
|
||||
DateTime :created_at
|
||||
DateTime :updated_at
|
||||
end
|
||||
|
||||
alter_table(:item_comments) do
|
||||
add_foreign_key :item_id, :items
|
||||
end
|
||||
end
|
||||
|
||||
down do
|
||||
drop_table(:item_comments)
|
||||
end
|
||||
|
||||
end
|
@ -1,5 +1,5 @@
|
||||
class Item < Sequel::Model
|
||||
|
||||
|
||||
one_to_many :item_comments
|
||||
|
||||
end
|
||||
|
5
lib/models/item_comment.rb
Normal file
5
lib/models/item_comment.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class ItemComment < Sequel::Model
|
||||
|
||||
many_to_one :items
|
||||
|
||||
end
|
@ -44,3 +44,12 @@ get '/item/:item_id' do
|
||||
:item => item
|
||||
}
|
||||
end
|
||||
|
||||
post '/item/:item_id/comment' do
|
||||
item = Item.first(id: params[:item_id])
|
||||
|
||||
comment = ItemComment.create(body: params[:comment_body])
|
||||
item.add_item_comment(comment)
|
||||
|
||||
redirect "/item/#{item.id}"
|
||||
end
|
||||
|
1
raven.rb
1
raven.rb
@ -22,6 +22,7 @@ Sequel::Model.plugin :timestamps
|
||||
DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database'))
|
||||
# Load models
|
||||
require_relative 'lib/models/item.rb'
|
||||
require_relative 'lib/models/item_comment.rb'
|
||||
|
||||
# Load helper functions
|
||||
require_relative 'lib/helpers.rb'
|
||||
|
@ -34,3 +34,28 @@
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="twelve columns">
|
||||
<% if item.item_comments.length > 0 %>
|
||||
<ul class="u-full-width">
|
||||
<% item.item_comments.each do |comment| %>
|
||||
<li><%= comment.body %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% else %>
|
||||
<p>There are no comments to display at this time.</p>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="twelve columns">
|
||||
<form action="/item/<%= item.id %>/comment" method="POST" class="u-full-width">
|
||||
<label for="comment_body">Add a comment:</label>
|
||||
<textarea name="comment_body" id="comment_body" class="u-full-width" cols="30" rows="10"></textarea>
|
||||
|
||||
<input class="button button-primary" type="submit" value="Submit">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user