From 45094a86bc430edeb8effd84c3f5d6143c43d40d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:49:22 +0200 Subject: [PATCH] adds author to debates [#11] --- app/controllers/debates_controller.rb | 7 +++++-- app/models/debate.rb | 5 ++++- app/views/debates/_debate.html.erb | 5 ++++- app/views/debates/show.html.erb | 5 ++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 53545887e..718772015 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -1,6 +1,7 @@ class DebatesController < ApplicationController before_action :set_debate, only: [:show, :edit, :update] - + before_action :authenticate_user!, only: [:new, :create] + def index @debates = Debate.all end @@ -16,7 +17,9 @@ class DebatesController < ApplicationController end def create - @debate = Debate.create(debate_params) + @debate = Debate.new(debate_params) + @debate.author = current_user + @debate.save respond_with @debate end diff --git a/app/models/debate.rb b/app/models/debate.rb index c66913c59..22e15fe3e 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,6 +1,9 @@ class Debate < ActiveRecord::Base + belongs_to :author, class_name: 'User', foreign_key: 'author_id' + validates :title, presence: true validates :description, presence: true + validates :author, presence: true validates :terms_of_service, acceptance: { allow_nil: false }, on: :create -end +end \ No newline at end of file diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index 4f99c9df9..637c0086c 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -1,6 +1,9 @@

<%= link_to debate.title, debate %>

<%= debate.description %>

-

Creado el: <%= l debate.created_at.to_date %>

+

+ Creado el: <%= l debate.created_at.to_date %> + por: <%= debate.author.name %> +



\ No newline at end of file diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 777b22d1b..f0a67e527 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -1,7 +1,10 @@

<%= @debate.title %>

<%= @debate.description %>

-

Creado el: <%= l @debate.created_at.to_date %>

+

+ Creado el: <%= l @debate.created_at.to_date %> + por: <%= @debate.author.name %> +

<%= link_to 'Edit', edit_debate_path(@debate) %> |