Rails form fields passing old data to whitelisted params -
update: fixed, pointing out kjmagic13. can see solution below.
so i've been working on 1 since yesterday. have rails project form_for collecting client data name, age, etc. there's "add spouse?" button uses javascript reveal additional spouse input fields. of these attributes whitelisted, , spouse data no more attributes on client model (no spouse object).
the first time input data these fields, pass through fine , saved. when revisit page, form prepopulated data. can edit fields , update client info, not spouse fields --> though fields whitelisted , nothing more client attributes. can't imagine how i'd make happen on purpose, less figure out how fix it.
the form gets submitted, , params passed in old spouse data, not went input field. new client info updated in same form. looking in logs shows old spouse data being passed through, if never typed anything
here's relevant snippets:
in clients_controller
def update if @client.report_ready? outdate_report end respond_to |format| if @client.update(client_params) format.html { redirect_to @client, notice: 'client updated.' } format.json { render :show, status: :ok, location: @client } else format.html { render :show } format.json { render json: @client.errors, status: :unprocessable_entity } end end end def client_params params.require(:client).permit(:first_name, :last_name, :email, :dob, :address, :city, :state, :zip, :phone, :retire_age, :ss_age, :user_id, :spouse_dob, :spouse_retire_age, :spouse_ss_age, :spouse_first_name, :spouse_last_name) end # form in view <%= form_for (@client) |f| %> <% if @client.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@client.errors.count, "error") %> prohibited plan being saved:</h2> <ul> <% @client.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div> <div> <%= f.label :first_name, class: 'left' %><br> <%= f.text_field :first_name %> </div> <div> <%= f.label :last_name, class: 'left' %><br> <%= f.text_field :last_name %> </div> <div> <%= f.label :email, class: 'left' %><br> <%= f.email_field :email %> </div> <div> <%= f.label :phone, class: 'left' %><br> <%= f.phone_field :phone %> </div> <div> <%= f.label :dob, 'birthdate', class: 'left' %> <%= f.date_field :dob, style: "font-size: .9em;" %> </div> <div> <% if @client.dob.present? %> <%= f.label :age, class: 'left' %><br> <%= text_field_tag :age, @client.age, readonly: true %> <% else %> <%= hidden_field :age, value: nil %> <% end %> </div> <div> <%= f.label :retire_age, 'ret. age', class: 'left' %><br> <%= f.number_field :retire_age %> </div> <div> <%= f.label :ss_age, 's.s. age', class: 'left' %><br> <%= f.number_field :ss_age %> </div> <div> <%= f.submit "save", class: "button radius tiny" %> </div> <% if !@client.new_record? && @client.has_spouse? %> <%= render 'spouse_fields', f: f %> <% else %> <%= render 'spouse_button' %> <% end %> <span id="spouse_fields" style="display:none"> <%= render 'spouse_fields', f: f %> </span> # spouse fields (which continuation of form) <hr> <h1>spouse information</h1> <div> <%= f.label :spouse_first_name, 'first name', class: 'left' %><br> <%= f.text_field :spouse_first_name %> </div> <div> <%= f.label :spouse_last_name, 'last name', class: 'left' %><br> <%= f.text_field :spouse_last_name %> </div> <div> <%= f.label :spouse_dob, 'date of birth', class: 'left' %><br> <%= f.date_field :spouse_dob, style: "font-size: .9em;" %> </div> <div> <% if @client.spouse_dob.present? %> <%= f.label :spouse_age, 'age', class: 'left' %><br> <%= text_field_tag :spouse_age, @client.spouse_age, readonly: true%> <% else %> <%= hidden_field :spouse_age, value: nil %> <% end %> </div> <div> <%= f.label :spouse_retire_age, 'ret. age', class: 'left' %><br> <%= f.number_field :spouse_retire_age %> </div> <div> <%= f.label :spouse_ss_age, 's.s. age', class: 'left' %><br> <%= f.number_field :spouse_ss_age %> </div> <div> <%= f.submit "save", class: "button radius tiny" %> </div> # js 'add spouse button' (just reveals fields, disappears) <text id="spouse_button" onclick="togglefields()"> add spouse? </text> <script> function togglefields() { document.getelementbyid("spouse_fields").style.csstext = ""; document.getelementbyid("spouse_button").style.csstext = "display:none"; }; </script>
fix:
all had missed location of hidden spouse fields. needed moved if/else block above it, (but kjmagic13's 2-line solution better):
<% if !@client.new_record? && @client.has_spouse? %> <%= render 'spouse_fields', f: f %> <% else %> <%= render 'spouse_button' %> <span id="spouse_fields" style="display:none"> <%= render 'spouse_fields', f: f %> </span> <% end %>
you're duplicating spouse_fields
partial when editing record. there 2 elements id of spouse_fields
, javascript toggle first 1 comes across. other remain hidden , fields being passed server.
<% if !@client.new_record? && @client.has_spouse? %> <%= render 'spouse_fields', f: f # renders here on edit %> <% else %> <%= render 'spouse_button' %> <% end %> <span id="spouse_fields" style="display:none"> <%= render 'spouse_fields', f: f # , renders here on edit %> </span>
you'll want more along lines of this:
<%= render 'spouse_button' unless @client.has_spouse? %> <%= content_tag :span, render('spouse_fields', f: f), style: ( @client.has_spouse? ? nil : 'display:none' ) -%>
Comments
Post a Comment