currency - Money Rails Gem - null values -
i have monetised 2 models of rails 4 app money-rails gem.
one called participants, other called funding. each of these models nested inside model, called scope. scope belongs project.
the associations are:
project has 1 scope; scope belongs project scope has 1 participant , has 1 funding; each of participant , funding belong scope.
project accepts nested attributes scope. scope accepts nested attributes participant , funding.
params each relevant attribute in participant , funding permitted in scope , project controllers models themselves. params scope permitted in scope , project controllers.
in project form, ask several questions. form has nested forms each of models belong it. inside scope form, ask users 2 boolean questions, being: want participants? want funding? each of these models has follow question participation cost , funding (those attributes monetised).
if answer questions true, reveal participant or funding form partial , ask how money want.
i have 2 problems:
first problem: not null violation 1. if user says want participants, there no associated costs, boolean question inside participant model asking whether there cost involved participation, error says:
error: null value in column "participation_cost_pennies" violates not-null constraint
- if user says don't want participants in answer question asked in scope form, same error in 1 above
second problem: if save amount in monetised fields, , come edit project form, form not show saved amount in monetised field - , if don't reenter it, error saying can't blank.
does know how to:
make first problem go away in circumstances except when participation costs sought; and
fix second problem displaying original amount saved when come edit form? have tried inserting :selected form element, doesn't anything.
my code follows:
inside scope form (nested inside project form):
<%= f.simple_fields_for :scope |s_all| %> <%= s_all.input :if_participant, :as => :boolean, :label => false, inline_label: 'public participants or volunteers' %> <%= s_all.input :if_funding, :as => :boolean, :label => false, inline_label: 'funding or expenses' %>
if answer these fields true, reveal partial forms participant of funding (for whichever true).
inside participants partial form, have:
<%= f.simple_fields_for :scope |participants_s| %> <%= participants_s.simple_fields_for :participant |par| %> <%= f.label 'are participants reimbursed costs?', :class => 'question-project' %> <%= par.collection_radio_buttons :costs, [[true, ' yes'], [false, ' no']], :first, :last, {:item_wrapper_class => 'fixradio'}, {:class => "response-project"} %> <%= f.label 'what amount pay participation costs?', :class => 'question-project' %> <%= par.select :participation_cost_currency, options_for_select(major_currencies(money::currency.table)), selected: :participation_cost_currency, label: false, prompt: "select costs currency" %> <%= par.input :participation_cost, label: false, placeholder: 'whole numbers only', selected: :participation_cost_pennies, :input_html => {:style => 'width: 250px; margin-top: 20px', class: 'response-project'} %>
for first problem, you'll want set default value participation_cost_cents
column in migration:
# in console > rails g migration change_default_for_participation_cost_cents # in migration file class changedefaultforparticipationcostcents < activerecord::migration def change change_column :participants, :participation_cost_cents, :integer, default: 0 end end
i'm not sure follow on second problem though. maybe should split question in two?
Comments
Post a Comment