Skip to content

Commit

Permalink
Refs #31: 5 author selections, wet
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBrendel committed Nov 3, 2017
1 parent 6eb0e73 commit 88f82ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/controllers/papers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions app/models/paper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions app/views/papers/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@
<%= f.text_field :year %>
</p>

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

<p>
<%= f.submit %>
</p>
Expand Down

0 comments on commit 88f82ca

Please sign in to comment.