Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/comments.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the comments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
86 changes: 86 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
class CommentsController < ApplicationController
# GET /comments
# GET /comments.json
def index
if params[:site_id].nil? or params[:site_id].empty?
@comments = Comment.all
else
@comments = Site.find(params[:site_id]).comments
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @comments }
end
end

# GET /comments/1
# GET /comments/1.json
def show
@comment = Comment.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @comment }
end
end

# GET /comments/new
# GET /comments/new.json
def new
@comment = Comment.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @comment }
end
end

# GET /comments/1/edit
def edit
@comment = Comment.find(params[:id])
end

# POST /comments
# POST /comments.json
def create
@comment = Comment.new(params[:comment])

respond_to do |format|
if @comment.save
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end

# PUT /comments/1
# PUT /comments/1.json
def update
@comment = Comment.find(params[:id])

respond_to do |format|
if @comment.update_attributes(params[:comment])
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end

# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment = Comment.find(params[:id])
@comment.destroy

respond_to do |format|
format.html { redirect_to comments_url }
format.json { head :no_content }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
4 changes: 4 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :site
end
1 change: 1 addition & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Site < ActiveRecord::Base
has_many :visits
has_many :trips, :through => :visits
has_attached_file :image
has_many :comments


# Debe estar protegido para evitar accesos indeseados
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class User < ActiveRecord::Base

has_many :sites
has_many :trips
has_many :comments

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
Expand Down
29 changes: 29 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<%= form_for(@comment) do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>

<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :comment %><br />
<%= f.text_field :comment %>
</div>
<div class="field">
<%= f.label :user_id %><br />
<%= f.number_field :user_id %>
</div>
<div class="field">
<%= f.label :site_id %><br />
<%= f.number_field :site_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing comment</h1>

<%= render 'form' %>

<%= link_to 'Show', @comment %> |
<%= link_to 'Back', comments_path %>
27 changes: 27 additions & 0 deletions app/views/comments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h1>Listing comments</h1>

<table>
<tr>
<th>Comment</th>
<th>User</th>
<th>Site</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @comments.each do |comment| %>
<tr>
<td><%= comment.comment %></td>
<td><%= comment.user_id %></td>
<td><%= comment.site_id %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Comment', new_comment_path %>
5 changes: 5 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New comment</h1>

<%= render 'form' %>

<%= link_to 'Back', comments_path %>
20 changes: 20 additions & 0 deletions app/views/comments/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<p id="notice"><%= notice %></p>

<p>
<b>Comment:</b>
<%= @comment.comment %>
</p>

<p>
<b>User:</b>
<%= @comment.user_id %>
</p>

<p>
<b>Site:</b>
<%= @comment.site_id %>
</p>


<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
10 changes: 6 additions & 4 deletions app/views/sites/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">

<td>
<%= link_to image_tag(site.image.url, :class => 'list_image'), site %>
<%= link_to image_tag(site.image_url, :class => 'list_image'), site %>
</td>

<td class="list_description">
Expand All @@ -19,16 +19,18 @@

<td class="list_actions">
<%= link_to 'Show', site %><br/>
<% if site.user == current_user %> <%= link_to 'Edit', edit_site_path(site) %><br/>
<% if site.user == current_user %> <%= link_to 'Edit', edit_site_path(site) %><br/>
<% if site.comments.length > 0 %> <%= link_to 'Comments', site_comments_path(site) %><br/>
<%= link_to 'Destroy', site,
:confirm => 'Are you sure?',
:method => :delete %>
<% end %> </td>
<% end %>
<% end %> </td>
</tr>
<% end %>
</table>
</div>

<br />

<%= link_to 'New site', new_site_path %>
<%= link_to 'New site', new_site_path %>
32 changes: 31 additions & 1 deletion app/views/sites/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<h1><%= @site.type.name if @site.type %></h1>

<%= image_tag(@site.image.url, :class => 'site_image') %>
<%= image_tag(@site.image_url, :class => 'site_image') %>

<h3><%= @site.name %></h3>

Expand All @@ -17,3 +17,33 @@

<p />
<% if @site.user == current_user %> <%= link_to 'Edit', edit_site_path(@site) %> |<% end %><%= link_to 'Back', sites_path %>

<div class="comentarios">
<table>
<tr>
<th>Comment</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @site.comments.each do |comment| %>
<tr>
<td><%= comment.comment %></td>
<td><%= comment.user.name if comment.user_id%></td>
<td><%= link_to 'Edit', edit_comment_path(comment) if comment.user == current_user %></td>
<td><%= link_to 'Destroy', comment, confirm: 'Are you sure?', method: :delete if comment.user == current_user %></td>
</tr>
<% end %>
</table>
</div>
<div class="new_comment">
<h2>Add a comment:</h2>
<%= form_for( @site.comments.build ) do |f| %>
<%= f.label :comment %><br />
<%= f.text_area :comment %>
<%= hidden_field_tag(:user_id, current_user) %>
<%= f.submit %>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/trips/_trip.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<% trip.visits.order(:hour).each do |visit| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
<td>
<%= link_to image_tag(visit.site.image.url, :class => 'list_image'), visit.site %>
<%= link_to image_tag(visit.site.image_url, :class => 'list_image'), visit.site %>
</td>

<td class="list_description">
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
Planet::Application.routes.draw do

resources :comments

resources :visits

resources :trips

devise_for :users

resources :sites
resources :sites do
resources :comments
end

resources :types do # Rutas anidadas /types/id/sites...,
resources :sites, :only => [ :index ] # Restringe a acción “index”
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20120415181005_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :comment
t.integer :user_id
t.integer :site_id

t.timestamps
end
end
end
10 changes: 9 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120411160519) do
ActiveRecord::Schema.define(:version => 20120415181005) do

create_table "comments", :force => true do |t|
t.string "comment"
t.integer "user_id"
t.integer "site_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "sites", :force => true do |t|
t.string "name"
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html

one:
comment: MyString
user_id: 1
site_id: 1

two:
comment: MyString
user_id: 1
site_id: 1
Loading