Skip to content

Commit

Permalink
Refs #31: 5 author selections, not so wet anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBrendel committed Nov 4, 2017
1 parent 61a9d8b commit 638ff29
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/controllers/papers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ 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]
ids = params.require(:paper)[:author_ids]
ids.each do |id|
@paper.authors << Author.find(id) if id && !id.to_s.empty?
end

Expand Down
20 changes: 0 additions & 20 deletions app/models/paper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,4 @@ class Paper < ActiveRecord::Base
validates :title, :venue, :year, presence: true
validates :year, numericality: { only_integer: true }
has_and_belongs_to_many :authors

def author_1
authors[0].id if authors[0]
end

def author_2
authors[1].id if authors[1]
end

def author_3
authors[2].id if authors[2]
end

def author_4
authors[3].id if authors[3]
end

def author_5
authors[4].id if authors[4]
end
end
14 changes: 4 additions & 10 deletions app/views/papers/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,10 @@
<%= f.text_field :year %>
</p>

<%= f.label 'Author 1', for: 'paper_author_id_1' %><br>
<%= f.collection_select(:author_1, Author.all, :id, :name, prompt: true) %><br>
<%= f.label 'Author 2', for: 'paper_author_id_2' %><br>
<%= f.collection_select(:author_2, Author.all, :id, :name, prompt: true) %><br>
<%= f.label 'Author 3', for: 'paper_author_id_3' %><br>
<%= f.collection_select(:author_3, Author.all, :id, :name, prompt: true) %><br>
<%= f.label 'Author 4', for: 'paper_author_id_4' %><br>
<%= f.collection_select(:author_4, Author.all, :id, :name, prompt: true) %><br>
<%= f.label 'Author 5', for: 'paper_author_id_5' %><br>
<%= f.collection_select(:author_5, Author.all, :id, :name, prompt: true) %>
<% for i in 1..5 do %>
<%= f.label ('Author ' + i.to_s), for: ('paper_author_id_' + i.to_s) %><br>
<%= f.collection_select :author_ids, Author.all, :id, :name, {selected: @paper.author_ids[i-1], include_blank: '(none)'}, {name: 'paper[author_ids][]', id: ('paper_author_id_' + i.to_s)} %><br>
<% end %>

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

0 comments on commit 638ff29

Please sign in to comment.