Added multiple forms for scientific and alternate names on crop creation

This commit is contained in:
AELOGICA committed 2015-07-17 07:31:51 +08:00
1 parent 472acd0e81
commit 12d151b68c
4 files changed
+81 -30

No files matched your search

+27 -12
View File
@@ -6,20 +6,35 @@ jQuery ->
$('.add-datepicker').datepicker('format' : 'yyyy-mm-dd')
$ ->
template = "<div id='template[INDEX]' class='template col-md-12'><div class='col-md-2'><label>Alternate name INDEX:</label></div><div class='col-md-8'><input class='form-control', id='alt_name[INDEX]')'></input><span class='help-block'>Alternate name of crop.</span></div><div class='col-md-2'></div></div>"
sci_template = "<div id='sci_template[INDEX]' class='template col-md-12'><div class='col-md-2'><label>Scientific name INDEX:</label></div><div class='col-md-8'><input name='sci_name[INDEX]' class='form-control', id='sci_name[INDEX]')'></input><span class='help-block'>Scientific name of crop.</span></div><div class='col-md-2'></div></div>"
index = 2
sci_index = $('#scientific_names .template').length + 1
$('#add-crop-row').click ->
compiled_input = $(template.split("INDEX").join(index))
$('#someform').append(compiled_input)
console.log("Added %d", index)
index = index + 1
$('#add-sci_name-row').click ->
compiled_input = $(sci_template.split("INDEX").join(sci_index))
$('#scientific_names').append(compiled_input)
sci_index = sci_index + 1
$('#remove-crop-row').click ->
if (index > 2)
index = index - 1
tmp = 'template[' + index + ']'
$('#remove-sci_name-row').click ->
if (sci_index > 2)
sci_index = sci_index - 1
tmp = 'sci_template[' + sci_index + ']'
element = document.getElementById(tmp)
element.remove()
console.log("Removed %s", tmp)
alt_template = "<div id='alt_template[INDEX]' class='template col-md-12'><div class='col-md-2'><label>Alternate name INDEX:</label></div><div class='col-md-8'><input name='alt_name[INDEX]' class='form-control', id='alt_name[INDEX]')'></input><span class='help-block'>Alternate name of crop.</span></div><div class='col-md-2'></div></div>"
alt_index = $('#alternate_names .template').length + 1
$('#add-alt_name-row').click ->
compiled_input = $(alt_template.split("INDEX").join(alt_index))
$('#alternate_names').append(compiled_input)
alt_index = alt_index + 1
$('#remove-alt_name-row').click ->
if (alt_index > 2)
alt_index = alt_index - 1
tmp = 'alt_template[' + alt_index + ']'
element = document.getElementById(tmp)
console.log("%s",tmp)
element.remove()
+26 -6
View File
@@ -94,9 +94,9 @@ class CropsController < ApplicationController
# GET /crops/new.json
def new
@crop = Crop.new
3.times do
@crop.scientific_names.build
end
@crop.alternate_names.build
@crop.scientific_names.build
respond_to do |format|
format.html # new.html.haml
format.json { render json: @crop }
@@ -106,15 +106,15 @@ class CropsController < ApplicationController
# GET /crops/1/edit
def edit
@crop = Crop.find(params[:id])
@crop.alternate_names.build if @crop.alternate_names.blank?
@crop.scientific_names.build if @crop.scientific_names.blank?
(3 - @crop.scientific_names.size).times do
@crop.scientific_names.build
end
end
# POST /crops
# POST /crops.json
def create
@crop = Crop.new(crop_params)
if current_member.has_role? :crop_wrangler
@@ -128,6 +128,12 @@ class CropsController < ApplicationController
respond_to do |format|
if @crop.save
params[:alt_name].each do |index, value|
alt_name = @crop.alternate_names.create(name: value, creator_id: current_member.id)
end
params[:sci_name].each do |index, value|
sci_name = @crop.scientific_names.create(scientific_name: value, creator_id: current_member.id)
end
unless current_member.has_role? :crop_wrangler
Role.crop_wranglers.each do |w|
Notifier.new_crop_request(w, @crop).deliver!
@@ -154,6 +160,20 @@ class CropsController < ApplicationController
respond_to do |format|
if @crop.update(crop_params)
@crop.alternate_names.each do |alt_name|
alt_name.destroy
end
params[:alt_name].each do |index, value|
alt_name = @crop.alternate_names.create(name: value, creator_id: current_member.id)
end
@crop.scientific_names.each do |sci_name|
sci_name.destroy
end
params[:sci_name].each do |index, value|
sci_name = @crop.scientific_names.create(scientific_name: value, creator_id: current_member.id)
end
if previous_status == "pending"
requester = @crop.requester
new_status = @crop.approval_status
+1 -1
View File
@@ -7,7 +7,7 @@ class Crop < ActiveRecord::Base
:allow_destroy => true,
:reject_if => :all_blank
has_many :alternate_names, after_add: :update_index, after_remove: :update_index
has_many :alternate_names, after_add: :update_index, after_remove: :update_index, dependent: :destroy
has_many :plantings
has_many :photos, :through => :plantings
has_many :seeds
+27 -11
View File
@@ -46,17 +46,33 @@
-# Everyone (wranglers and requesters) gets to add scientific names
%h2
Scientific names
= button_tag "+", :id => "add-crop-row", :type => "button"
= button_tag "-", :id => "remove-crop-row", :type => "button"
.form-group#someform
.template.col-md-12
.col-md-2
= label_tag :alternate_names, "Alternate name 1:", :class => 'control-label'
.col-md-8
= text_field_tag :name, nil, :id => 'alt_name[1]', :class => 'form-control'
%span.help-block Alternate name of crop.
.col-md-2
= button_tag "+", :id => "add-sci_name-row", :type => "button"
= button_tag "-", :id => "remove-sci_name-row", :type => "button"
.form-group#scientific_names
- @crop.scientific_names.each.with_index do |sci, index|
.template.col-md-12{ :id => "sci_template[#{index+1}]" }
.col-md-2
= label_tag :scientific_names, "Scientific name #{index+1}:", :class => 'control-label'
.col-md-8
= text_field_tag "sci_name[#{index+1}]", sci.scientific_name, :id => "sci_name[#{index+1}]", :class => 'form-control'
%span.help-block Scientific name of crop.
.col-md-2
%h2
Alternate names
= button_tag "+", :id => "add-alt_name-row", :type => "button"
= button_tag "-", :id => "remove-alt_name-row", :type => "button"
.form-group#alternate_names
- @crop.alternate_names.each.with_index do |alt, index|
.template.col-md-12{ :id => "alt_template[#{index+1}]" }
.col-md-2
= label_tag :alternate_names, "Alternate name #{index+1}:", :class => 'control-label'
.col-md-8
= text_field_tag "alt_name[#{index+1}]", alt.name, :id => "alt_name[#{index+1}]", :class => 'form-control'
%span.help-block Alternate name of crop.
.col-md-2
-# This is used for comments from crop requesters. We need to show it
-# to everyone, but we don't include it on new crops from wranglers.