From 88f82ca888adf88cd5ed29c4376753a653ec5874 Mon Sep 17 00:00:00 2001 From: Erik Brendel Date: Sat, 4 Nov 2017 00:02:52 +0100 Subject: [PATCH] Refs #31: 5 author selections, wet --- app/controllers/papers_controller.rb | 5 +++++ app/models/paper.rb | 20 ++++++++++++++++++++ app/views/papers/edit.html.erb | 11 +++++++++++ 3 files changed, 36 insertions(+) diff --git a/app/controllers/papers_controller.rb b/app/controllers/papers_controller.rb index 5f3617d..90af259 100644 --- a/app/controllers/papers_controller.rb +++ b/app/controllers/papers_controller.rb @@ -27,6 +27,11 @@ def create def update @paper = Paper.find(params[:id]) + @paper.authors = [] + for i in 1..5 + id = params.require(:paper)[('author' + i.to_s).to_sym] + @paper.authors << Author.find(id) if id && !id.to_s.empty? + end if @paper.update(paper_params) redirect_to @paper diff --git a/app/models/paper.rb b/app/models/paper.rb index 9592592..b6339c3 100644 --- a/app/models/paper.rb +++ b/app/models/paper.rb @@ -2,4 +2,24 @@ class Paper < ActiveRecord::Base validates :title, :venue, :year, presence: true validates :year, numericality: { only_integer: true } has_and_belongs_to_many :authors + + def author1 + authors[0].id if authors[0] + end + + def author2 + authors[1].id if authors[1] + end + + def author3 + authors[2].id if authors[2] + end + + def author4 + authors[3].id if authors[3] + end + + def author5 + authors[4].id if authors[4] + end end diff --git a/app/views/papers/edit.html.erb b/app/views/papers/edit.html.erb index 145f44b..827dade 100644 --- a/app/views/papers/edit.html.erb +++ b/app/views/papers/edit.html.erb @@ -31,6 +31,17 @@ <%= f.text_field :year %>

+ <%= f.label 'Author 1', for: 'paper_author_id_1' %>
+ <%= f.collection_select(:author1, Author.all, :id, :name, prompt: true) %>
+ <%= f.label 'Author 2', for: 'paper_author_id_2' %>
+ <%= f.collection_select(:author2, Author.all, :id, :name, prompt: true) %>
+ <%= f.label 'Author 3', for: 'paper_author_id_3' %>
+ <%= f.collection_select(:author3, Author.all, :id, :name, prompt: true) %>
+ <%= f.label 'Author 4', for: 'paper_author_id_4' %>
+ <%= f.collection_select(:author4, Author.all, :id, :name, prompt: true) %>
+ <%= f.label 'Author 5', for: 'paper_author_id_5' %>
+ <%= f.collection_select(:author5, Author.all, :id, :name, prompt: true) %> +

<%= f.submit %>