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/models/user.rb b/app/models/user.rb index c8220270d..9c55bc59e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,8 @@ class User < ActiveRecord::Base - # Include default devise modules. Others available are: - # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + + def name + "#{first_name} #{last_name}" + end end 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 %> +
Explica con todo el detalle que puedas y de una manera sencilla la idea y que crees que conseguirĂamos con ella
<%= f.text_area :description %> - <% if action_name == 'new' %> + <% if @debate.new_record? %> <%= f.check_box :terms_of_service %> Acepto la polĂtica de privacidad y el aviso legal <% end %> 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 %> +