I'm trying to only show errors in my form when the user clicks the submit button but currently, it is displaying all errors before the user clicks the submit button. How do I only show errors when the user submits the form?
I'm using simple-form in Rails
Here is my simple-form:
<div class="col-md-10 col-lg-8 col-xl-5 col-md-offset-4 mx-auto">
<%= simple_form_for #customer, url: customers_path, method: :post do |f| %>
<%= f.error_notification %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :email, input_html: { autocomplete: 'email' } %>
<%= f.input :budget, collection: ["€200,000 - €299,999", "€300,000 - €399,999", "€400,000 - €499,999", "€500,000 - €649,999", "€650,000 - €799,999", "€800,000 - €1,000,000", "€1,000,000 +"] %>
<%= f.input :comments, :as => :text, :input_html => { 'rows' => 10, 'cols' => 10 } %>
<%= f.button :submit, "Submit", class: "btn-primary trigger mt-1" %>
<% end %>
</div>
Here are my customer validations in my customer model:
class Customer < ApplicationRecord
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, uniqueness: true
validates :first_name, presence: true, length: { minimum: 2 }
validates :last_name, presence: true, length: { minimum: 2 }
validates :budget, presence: true
validates :comments, presence: true
end
Thank you
you can change simple_form_for (line 2) like this below
<%= simple_form_for #customer, html: { novalidate: true }, url: customers_path, method: :post do |f| %>
explanation:
by adding , html: { novalidate: true } This option adds a new novalidate property to the form, instructing it to skip all HTML 5 validation.
Please don't flag this as duplicate. I've already reviewed the other similar questions and mine is different.
I have an issue where some of the users that input their addresses manage to create duplicate CartAddress records.
My database structure includes a Cart model and CartAddress model.
The Cart model looks something like this:
class Cart < ApplicationRecord
has_many :cart_addresses, -> { order(address_type: :asc) }, inverse_of: :cart, dependent: :destroy
accepts_nested_attributes_for :cart_addresses, allow_destroy: true
end
My CartAddress model looks like this:
class CartAddress < ApplicationRecord
belongs_to :cart
validates :first_name, presence: true
validates :last_name, presence: true
validates :email, presence: true
validates :telephone, presence: true
validates :address_line_one, presence: true
validates :city, presence: true
validates :country, presence: true
validates :state, presence: true
validates :zip_code, presence: true
validates_format_of :email, :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
end
When the user loads the cart page:
def index
#bunch of init code here
#more here
#exists = true
if #cart.cart_addresses.count == 0
#exists = false
2.times { #cart.cart_addresses.build(country: "United States", state: "Alabama")}
end
end
Then when I create/update:
def update_addresses
#countries = Country.all
#states = State.all
#cart = Cart.find(params[:cart][:id])
if params[:cart][:cart_addresses_attributes]["0"][:address_type] == "Billing" and params[:cart][:cart_addresses_attributes]["1"][:address_type] == "Shipping"
#cart.update(params.require(:cart).permit(cart_addresses_attributes: [:id, :address_type, :first_name, :last_name, :email, :telephone, :address_line_one, :address_line_two, :city, :zip_code, :country, :state]))
#bunch of other code
end
Here is my partial that renders the form
<%= form_for(cart, :remote => true, :url => update_cart_addresses_path, :html => {:class => 'addresses-class', :id => 'cart-addresses', :autocomplete => :off}) do |u| %>
<% billing = true %>
<% state = 0 %>
<%= u.hidden_field :id %>
<% offset_two = true %>
<% if #exists == true %>
<%= u.fields_for :cart_addresses do |b| %>
<%= b.hidden_field :address_type %>
<% if billing == true %>
<div class="col-xs-12">
<h4>Billing Address</h4>
<% billing = false %>
</div>
<% else %>
<div class="col-xs-12 pad-top-15 checkout-section-padding">
<h4>Shipping Address</h4>
<input type="checkbox" id="same-as-billing" name="same_as_billing" value="1" onclick="toggleShippingAddress(this);">Same as Billing?
</div>
<% end %>
<div class="address">
<div class="col-xs-6">
<%= b.label :first_name %><span class="required-field">*</span>
<%= b.text_field :first_name, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''}%>
</div>
<div class="col-xs-6">
<%= b.label :last_name %><span class="required-field">*</span>
<%= b.text_field :last_name, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-6 pad-top-15">
<%= b.label :email %><span class="required-field">*</span>
<%= b.email_field :email, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-6 pad-top-15">
<%= b.label :telephone %><span class="required-field">*</span>
<%= b.text_field :telephone, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.label :address_line_one %><span class="required-field">*</span>
<%= b.text_field :address_line_one, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.text_field :address_line_two, class: 'checkout-addr-text-field', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.label :city %><span class="required-field">*</span>
<%= b.text_field :city, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.label :country %><span class="required-field">*</span>
<%= b.select(:country, countries.all.collect {|a| [a.name]}, {:prompt => "Select A Country"}, :onchange => "setStates(#{state});", class: 'checkout-country') %>
</div>
<div class="col-xs-6 pad-top-15">
<div id="<%= "state_" + state.to_s %>">
<%= b.label :state %><span class="required-field">*</span>
<%= b.text_field :state, class: 'checkout-addr-text-field' %>
</div>
</div>
<div class="col-xs-6 pad-top-15">
<%= b.label :zip_code%><span class="required-field">*</span>
<%= b.text_field :zip_code, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<% state = state + 1 %>
</div>
<% end %>
<% else %>
<%= u.fields_for :cart_addresses do |b| %>
<% if billing == true %>
<div class="col-xs-12">
<h4>Billing Address</h4>
<% billing = false %>
<%= b.hidden_field :address_type, value: "Billing" %>
</div>
<% else %>
<div class="col-xs-12 pad-top-15 checkout-section-padding">
<h4>Shipping Address</h4>
<input type="checkbox" id="same-as-billing" name="same_as_billing" value="1" onclick="toggleShippingAddress(this);">Same as Billing?
<%= b.hidden_field :address_type, value: "Shipping" %>
</div>
<% end %>
<div class="address">
<div class="col-xs-6">
<%= b.label :first_name %><span class="required-field">*</span>
<%= b.text_field :first_name, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-6">
<%= b.label :last_name %><span class="required-field">*</span>
<%= b.text_field :last_name, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-6 pad-top-15">
<%= b.label :email %><span class="required-field">*</span>
<%= b.email_field :email, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-6 pad-top-15">
<%= b.label :telephone %><span class="required-field">*</span>
<%= b.text_field :telephone, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.label :address_line_one %><span class="required-field">*</span>
<%= b.text_field :address_line_one, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.text_field :address_line_two, class: 'checkout-addr-text-field', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.label :city %><span class="required-field">*</span>
<%= b.text_field :city, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<div class="col-xs-12 pad-top-15">
<%= b.label :country %><span class="required-field">*</span>
<%= b.select(:country, countries.all.collect {|a| [a.name]}, {:prompt => "Select A Country"}, :onchange => "setStates(#{state});", class: 'checkout-country') %>
</div>
<div class="col-xs-6 pad-top-15">
<div id="<%= "state_" + state.to_s %>">
<%= b.label :state %><span class="required-field">*</span>
<%= b.text_field :state, class: 'checkout-addr-text-field' %>
</div>
</div>
<div class="col-xs-6 pad-top-15">
<%= b.label :zip_code %><span class="required-field">*</span>
<%= b.text_field :zip_code, class: 'checkout-addr-text-field required', :data => {:hj_whitelist => ''} %>
</div>
<% state = state + 1 %>
</div>
<% end %>
<% end %>
<% end %>
<div class="col-xs-12 pad-top-15">
<button id="update-checkout-addresses-btn" class="checkout-addresses-submit-btn btn btn-primary btn-sm" onclick="submitCheckoutAddressesForm(this);" type="button">Update Addresses</button>
</div>
When creating new address records the form is submitted without an id. When editing the records a id is submitted. Somehow it appears that the form is being submitted more than once although I do have javascript disabling the form upon submission. How should I handle this in the controller to prevent this error from happening? Or fix my view partial?
UPDATE
Here is my JS
function submitCheckoutAddressesForm(element){
element.disabled = true;
var e = document.getElementById("same-as-billing")
sameAsBillingCheckout(e);
$(e).closest("form").submit();
}
The below function makes sure that all the elements are the same in the form if a checkbox "Shipping Address Same As Billing" is selected. The reason I am having to clone the element and copy it into the shipping addresses is because the element type has the ability to change due to other javascript. I figured this would be the safest way of handling this event just to ensure that the proper elements were submitted in the form. I don't see that this should effect the duplicate records being created.
function sameAsBillingCheckout(element){
if(element.checked == true){
var billing = ["#cart_cart_addresses_attributes_0_first_name", "#cart_cart_addresses_attributes_0_last_name", "#cart_cart_addresses_attributes_0_email", "#cart_cart_addresses_attributes_0_telephone",
"#cart_cart_addresses_attributes_0_address_line_one", "#cart_cart_addresses_attributes_0_address_line_two", "#cart_cart_addresses_attributes_0_country", "#cart_cart_addresses_attributes_0_state", "#cart_cart_addresses_attributes_0_city", "#cart_cart_addresses_attributes_0_zip_code"]
var shipping = ["#cart_cart_addresses_attributes_1_first_name", "#cart_cart_addresses_attributes_1_last_name", "#cart_cart_addresses_attributes_1_email", "#cart_cart_addresses_attributes_1_telephone",
"#cart_cart_addresses_attributes_1_address_line_one", "#cart_cart_addresses_attributes_1_address_line_two", "#cart_cart_addresses_attributes_1_country", "#cart_cart_addresses_attributes_1_state", "#cart_cart_addresses_attributes_1_city", "#cart_cart_addresses_attributes_1_zip_code"]
for(i = 0; i < billing.length; i++){
$(shipping[i]).val($(billing[i]).val());
if(billing[i] == "#cart_cart_addresses_attributes_0_country"){
$("#cart_cart_addresses_attributes_1_state").remove();
var e = document.getElementById("cart_cart_addresses_attributes_0_state");
var cln = e.cloneNode(true);
cln.name = "cart[cart_addresses_attributes][1][state]";
cln.id = "cart_cart_addresses_attributes_1_state";
var stateDiv = document.getElementById("state_1");
stateDiv.appendChild(cln);
}
}
}
}
This code below does the same as the javascript above. It's just submitting the form after the user finishes typing if the form isn't disabled.
$(function(){
//create one instance for handler:
var myHandler = function(e){
var sAB = document.getElementById("same-as-billing").checked;
var ready = true;
//LOOPS THROUGH ALL THE TEXT FIELDS
$('.checkout-addr-text-field').each(function (){
//IF SAME AS BILLING CHECKBOX SELECTED
if(sAB == true){
//if the field is disabled or the field includes _0_ (means its a billing address field) and the id is not the optional field and the value isn't blank then dont submit the form.
if(this.disabled || this.id.indexOf("_0_") !== -1 && this.id !=
"cart_cart_addresses_attributes_0_address_line_two" && this.value == ""){
ready = false;
}
}else{ // IF SAME AS BILLING CHECKBOX NOT SELECTED
if(this.disabled || this.id !=
"cart_cart_addresses_attributes_0_address_line_two" && this.id !=
"cart_cart_addresses_attributes_1_address_line_two" && this.value == ""){
ready = false;
}
}
});
if(ready == true){
var e = document.getElementById("same-as-billing")
sameAsBillingCheckout(e);
$(e).closest("form").submit();
$("#cart-addresses :input").attr("disabled", true);
}
};
$(document).on('keyup', '.checkout-addr-text-field', debounce(function(e){
myHandler(e);
}, 1700));
$(document).on('change', '#cart_cart_addresses_attributes_0_state', function(e){
myHandler(e);
});
$(document).on('change', '#cart_cart_addresses_attributes_1_state', function(e){
myHandler(e);
});
$(document).on('change', '#cart_cart_addresses_attributes_0_state', function(e){
myHandler(e);
});
$(document).on('click', '#same-as-billing', function(e){
if(this.checked){
myHandler(e);
}
});
});
Here is code sample:
<%= form_for #article, html: { class: "form-horizontal" } do |f| %>
<div class="form-group">
<%= f.label :keywords, class: 'col-md-1 control-label' %>
<div class="col-md-3">
<%= f.select :keywords, ['test_1', 'test_2', 'test_3', 'test_4', 'test_5'], {}, { :multiple => true, :size => 10, :class => 'form-control' } %>
</div>
</div>
<% end %>
When I set multiple to 'false' it works just fine, but if I set it to true (for multiple select), it just doesn't pass any data. If I have verification it gives me an "keyword is empty" error and if I remove validation - it is just empty. Any ideas?
<%= f.select :keywords, options_for_select([['test_1','test_1'], ['test_2','test_2'], ['test_3','test_3'], ['test_4','test_4'], ['test_5','test_5']]), {}, { :multiple => true, :size => 10, :class => 'form-control' } %>
I'm newbie and need help on reviewing my code.
The form data can't be saved and flash success message is not showing either.
this is what's showing up in rails server:
Started POST "/registrations" for 139.193.122.192 at 2016-01-18 02:02:07 +0000
Cannot render console from 139.193.122.192! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"rOZfP2y2huPWAZoUaYjlRXyos5aqiaJh3MTiidXZFRBHJW0yL/rSC4cZwyMuZ//UwwUounNMgzL8HL/yJI8RyQ==", "registration"=>{"name"=>"Yosafat", "last_name"=>"Kakomba", "date_of_birth"=>"06/08/1982", "address"=>"Perum Vila Nusa Indah 2 Blok CC2/18 Jatiasih", "suburb"=>"Bekasi", "post_code"=>"16969", "telephone"=>"622193709536", "email"=>"ariel.kakomba#gmail.com", "news_letter"=>"1", "private_health_fund"=>"0", "exercise_routine"=>"Never", "occupation"=>"Programmer", "past_medical_history"=>"I don't have", "medication"=>"I don't have", "symptoms"=>"I don't have", "terms_of_service"=>"1"}, "gift_voucher"=>"1", "other_references"=>{"{:placeholder=>\"Do you have any other references?\"}"=>""}, "gentle_treatment"=>"1", "head"=>"1", "allergies_or_asthma"=>"1", "commit"=>"Register Now"}
Unpermitted parameter: last_name (0.1ms) begin transaction Registration Exists (0.2ms) SELECT 1 AS one FROM "registrations" WHERE LOWER("registrations"."email") = LOWER('ariel.kakomba#gmail.com') LIMIT 1 (0.1ms) rollback transaction Redirected to https://simpleform3-yoskakomba.c9users.io/ Completed 302 Found in 192ms (ActiveRecord: 1.7ms)
Started GET "/" for 139.193.122.192 at 2016-01-18 02:02:07 +0000 Cannot render console from 139.193.122.192! Allowed networks:
127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PagesController#index as HTML Rendered pages/index.html.erb within layouts/application (37.3ms) Rendered layouts/_navigation.html.erb (2.3ms) Rendered layouts/_messages.html.erb (0.1ms) Rendered layouts/_footer.html.erb (0.1ms) Completed 200 OK in 309ms (Views:
308.1ms | ActiveRecord: 0.0ms)
and when I checked in Rails Console:
The data is not saved, and flash success message is not showing up.
Here is my controller:
class RegistrationsController < ApplicationController
def new
#registration = Registration.new
end
def create
#registration = Registration.new(registration_params)
if #registration.save
flash[:success] = "Your registration was created succesfully"
redirect_to root_path
else
redirect_to root_path
end
end
private
def registration_params
params.require(:registration).permit(:name, :lastname, :date_of_birth,
:address, :suburb, :post_code, :telephone, :private_health_fund,
:email, :reference, :news_letter, :occupation, :exercise_routine, :body_conditions,
:past_medical_history, :medication, :symptoms, :other_experience, :preference,
:body_area, :terms_of_service, :drive_and_walk_by, :google_search, :gift_voucher, :yellow_pages, :yelp, :trip_advisor,:start_local,
:tenbest, :time_out, :friend_referral, :quiet, :treatment_description, :gentle_treatment, :face,
:hair_scalp, :head, :chest, :stomach, :back, :buttocks, :arms, :legs, :hands, :feet, :allergies_or_asthma, :open_wounds,
:numbness_tinglint,
:skin_condition, :headaches_migraine, :recent_illness_or_surgery, :varicose_veins, :diabetes, :osteoporosis)
end
end
the form is in index.html.erb and using ClientSideValidation gem to validate
<h2>Registration Form</h2> <hr>
<div class="row">
<div class="well col-md-8 col-md-offset-2">
<%= form_for #registration, validate:true do |f| %>
<div class="row"><!-- FIELD FOR NAME / LAST NAME / DOB -->
<div class="col-md-4">
<%= f.label :name %>
<%= f.text_field :name, placeholder: 'first name' %>
</div>
<div class="col-md-4">
<%= f.label :last_name %>
<%= f.text_field :last_name, placeholder: 'last name' %>
</div>
<div class="col-md-4">
<%= f.label :date_of_birth %>
<%= f.text_field :date_of_birth, placeholder: 'Your date of birth ex: DD/MM/YYYY' %>
</div>
</div>
<div class="row"><!-- FIELD FOR ADDRESS / SUBURB/ POSTCODE -->
<div class="col-md-4">
<%= f.label :address %>
<%= f.text_field :address, placeholder: 'Your primary home address' %>
</div>
<div class="col-md-4">
<%= f.label :suburb %>
<%= f.text_field :suburb, placeholder: 'Your sub urban area' %>
</div>
<div class="col-md-4">
<%= f.label :post_code %>
<%= f.text_field :post_code, placeholder: 'Address postal code' %>
</div>
</div>
<div class="row"><!-- FIELD FOR TELEPHONE AND EMAIL -->
<div class="col-md-6">
<%= f.label :telephone %>
<%= f.text_field :telephone, placeholder: 'Mobile or landline' %>
</div>
<div class="col-md-6">
<%= f.label :email %>
<%= f.email_field :email, placeholder: 'Your email address' %>
</div>
</div>
<div class="row checkbox"><!-- FIELD FOR PRIVATE HEALTH FUND & NEWSLETTER CHECKBOX -->
<div class="col-md-6">
<%= f.label :news_letter %>
<%= f.check_box :news_letter %>
<p>*Interested in receiving news or promotion from us?</p>
</div>
<div class="col-md-6">
<%= f.label :private_health_fund %>
<%= f.check_box :private_health_fund %>
<p>*Do you have private health fund?</p>
</div>
</div>
<div class="row"></div>
<div class="row checkbox"><!-- FIELD FOR REFERENCES AND PREFERENCE -->
<div class="col-md-6">
<b>How Did You Hear About Us?</b><br/><br/ >
<%= check_box_tag :drive_and_walk_by %> <h8>Drive and Walk by</h8> <br/>
<%= check_box_tag :google_search %> <h8>Google Search</h8> <br/>
<%= check_box_tag :gift_voucher %> <h8>Gift voucher</h8> <br/>
<%= check_box_tag :yellow_pages %> <h8>Google Search</h8> <br/>
<%= check_box_tag :yelp %> <h8>Yelp</h8> <br/>
<%= check_box_tag :trip_advisor %> <h8>Trip Advisor</h8> <br/>
<%= check_box_tag :start_local %> <h8>Start Local</h8> <br/>
<%= check_box_tag :tenbest %> <h8>10Best</h8> <br/>
<%= check_box_tag :time_out %> <h8>Time Out</h8> <br/>
<%= check_box_tag :friend_referral %> <h8>Friend referral</h8> <br/><br/>
<b>Other References</b><br/>
<%= text_field :other_references, placeholder: 'Do you have any other references?'%>
</div>
<div class="col-md-6">
<b>Your Preference in Massage Therapy</b><br/><br/ >
<%= check_box_tag :quiet %> <h8>Quiet environment</h8> <br/>
<%= check_box_tag :treatment_description %> <h8>Treatment Description</h8> <br/>
<%= check_box_tag :gentle_treatment %> <h8>Gentle Treatment</h8> <br/><br/>
<b>Excercise Routine</b><br/>
<%= f.select :exercise_routine, ['Never', 'Light 1-2 times', 'Moderate 3-4 times', 'Competitive 5-7 times'], validate: false, prompt: 'How many times do you excercise?' %>
</div>
</div>
<div class="row checkbox">
<div class="col-md-6">
<b>Body area you want to skip</b><br/><br/ >
<%= check_box_tag :face %> <h8>Face</h8> <br/>
<%= check_box_tag :hair_scalp %> <h8>Hair Scalp</h8> <br/>
<%= check_box_tag :head %> <h8>Head</h8> <br/>
<%= check_box_tag :chest %> <h8>Chest</h8> <br/>
<%= check_box_tag :stomach %> <h8>Stomach</h8> <br/>
<%= check_box_tag :back %> <h8>Back</h8> <br/>
<%= check_box_tag :buttocks %> <h8>Buttocks</h8> <br/>
<%= check_box_tag :arms %> <h8>Arms</h8> <br/>
<%= check_box_tag :legs %> <h8>Legs</h8> <br/>
<%= check_box_tag :hands %> <h8>Hands</h8><br/>
<%= check_box_tag :feet %> <h8>Feet</h8><br/>
</div>
<div class="col-md-6">
<b>Existing Health Condition</b><br/><br/ >
<%= check_box_tag :allergies_or_asthma %> <h8>Allergies or Asthma</h8> <br/>
<%= check_box_tag :open_wounds %> <h8>Open Wounds</h8> <br/>
<%= check_box_tag :numbness_tinglint %> <h8>Numbness/Tinglint</h8> <br/>
<%= check_box_tag :skin_condition %> <h8>Skin Condition</h8> <br/>
<%= check_box_tag :headaches_migraine %> <h8>Headaches/Migraine</h8> <br/>
<%= check_box_tag :recent_illness_or_surgery %> <h8>Recent Illness or Surgery</h8> <br/>
<%= check_box_tag :varicose_veins %> <h8>Varicose Veins</h8> <br/>
<%= check_box_tag :diabetes %> <h8>Diabetes</h8> <br/>
<%= check_box_tag :osteoporosis %> <h8>Osteoporosis</h8> <br/><br/>
</div>
</div><br/>
<div class="row"><!-- FIELD FOR OCCUPATION AND PAST MEDICAL HISTORY -->
<div class="col-md-6">
<%= f.label :occupation %>
<%= f.text_area :occupation, placeholder: "Your main job", validate: false %>
</div>
<div class="col-md-6">
<%= f.label :past_medical_history %>
<%= f.text_area :past_medical_history, placeholder: "Please tell us if you have medication history within 5 years before we proceed your therapy",
validate: false %>
</div>
</div>
<div class="row"><!-- FIELD FOR MEDICATION AND SYMPTOMS -->
<div class="col-md-6">
<%= f.label :medication %>
<%= f.text_area :medication, placeholder: "Please tell us if you have medication history within 5 years before we proceed your therapy",
validate: false %>
</div>
<div class="col-md-6">
<%= f.label :symptoms %>
<%= f.text_area :symptoms, placeholder: "Please tell us if you have any symptoms regarding your existing health condition before we proceed your therapy", validate: false %>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<b>Terms Of Service</b><br/>
<h7>Arrival Time</h7>
<p>Please arrive at least 10 minutes before your treatment. New clients will be asked to fill out a record form.</p>
<h7>Arrival Late</h7>
<p>All treatments conclude at the scheduled time.</p>
<h7>Cancelation Policy<h7>
<p>When you reschedule or cancel an appointment, we require 24 hours notice; cancellation without notice will incur a cancellation fee of $30.
Confirmation of an appointment is acknowledgement of this cancellation policy.</p>
</div>
</div>
<div class="row">
<div class="col-md-6 col-md-offset-3 checkbox">
<%= f.label "I agree with the terms of service" %>
<%= f.check_box :terms_of_service %><br/>
<p>*I read and understood the terms of service</p>
</div>
</div>
<%= f.submit 'Register Now', class: "btn btn-success" %>
<% end %>
</div><!-- FIELD FOR CLASS WELL -->
**This is my *application.html.erb***
<!DOCTYPE html>
<html>
<head>
<title>Workspace</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render 'layouts/navigation' %>
<div class="container">
<%= render 'layouts/messages' %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>
</html>
This is my registration.rb
class Registration < ActiveRecord::Base validates :name, presence: true, length: { minimum: 5, maximum: 19 } validates :last_name, presence: true, length: { minimum: 5, maximum: 19 } validates :address, presence: true validates :date_of_birth, presence: true VALID_EMAIL_REGEX = /\A([\w+\-].?)+#[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 105 },
uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX } validates :suburb, presence: true validates :state, presence: true validates :post_code, presence: true validates :telephone, presence: true validates :reference, presence: true validates :news_letter, presence: true validates :occupation, presence: true validates :exercise_routine, presence: true validates :body_conditions, presence: true validates :past_medical_history, presence: true, length: { minimum: 5, maximum: 500 } validates :medication, presence: true, length: { minimum: 5, maximum: 500 } validates :symptoms, presence: true, length: { minimum: 5, maximum: 500 } validates :other_experience, presence: true, length: { minimum: 5, maximum: 500 } validates :preference, presence: true validates :body_area, presence: true validates :terms_of_service, presence: true end
Thank you so much for your help
When you look into the log that you posted, you can see there is an Unpermitted parameter: last_name. This is because in the form you have last_name and in the registration_params method you have lastname.
Changing lastname to last_name in registration_params should solve your problem.
Since you're new, I have some suggestions (#Pavan's answer is correct).
The reason you've got the problem is because you're requiring :last_name to be present with a validation:
#app/models/registration.rb
class Registration < ActiveRecord::Base
validates :name, presence: true, length: { minimum: 5, maximum: 19 }
validates :last_name, presence: true, length: { minimum: 5, maximum: 19 }
validates :address, presence: true
validates :date_of_birth, presence: true VALID_EMAIL_REGEX = /\A([\w+\-].?)+#[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 105 }, uniqueness: { case_sensitive: false }, format: { with: VALID_EMAIL_REGEX }
validates :suburb, presence: true
validates :state, presence: true
validates :post_code, presence: true
validates :telephone, presence: true
validates :reference, presence: true
validates :news_letter, presence: true
validates :occupation, presence: true
validates :exercise_routine, presence: true
validates :body_conditions, presence: true
validates :past_medical_history, presence: true, length: { minimum: 5, maximum: 500 }
validates :medication, presence: true, length: { minimum: 5, maximum: 500 }
validates :symptoms, presence: true, length: { minimum: 5, maximum: 500 }
validates :other_experience, presence: true, length: { minimum: 5, maximum: 500 }
validates :preference, presence: true
validates :body_area, presence: true
validates :terms_of_service, presence: true
end
Although this is great, it means that if you don't pass that parameter, Rails will come back with an error. Errors are stored in the errors method attached to your returned object:
#app/views/registrations/new.html.erb
<%= form_for #registration do |f| %>
<% if #registration.errors.any? %>
<% #registration.errors.full_messsages do |message| %>
<%= message %>
<% end %>
<% end %>
<% end %>
This will return the error messages from your model.
#app/controllers/registrations_controller.rb
class RegistrationsController < ApplicationController
def create
#registration = Registration.new(registration_params)
redirect_to root_path, success: "Your registration was created" if #registration.save
# will render "new" if errors
end
end
Secondly, you can clean up your model massively by declaring multiple validations (if they're the same):
#app/models/registration.rb
class Registration < ActiveRecord::Base
VALID_EMAIL_REGEX = /\A([\w+\-].?)+#[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
validates :name, :last_name, presence: true, length: { minimum: 5, maximum: 19 }
validates :email, presence: true, length: { maximum: 105 }, uniqueness: { case_sensitive: false }, format: { with: VALID_EMAIL_REGEX }
validates :past_medical_history, :medication, :symptoms, :other_experience, presence: true, length: { minimum: 5, maximum: 500 }
validates :date_of_birth, :address, :suburb, :state, :post_code, :telephone, :reference, :news_letter, :occupation, :exercise_routine, :body_conditions, :preference, :body_area, :terms_of_service, presence: true
end
Thirdly, your model needs to be called by the object name it reflects.
Ruby is an object orientated language, which means that everything you do with the system should be scoped around objects.
As a beginner, this won't matter; as you progress, you'll realize the importance of having...
#user = User.find x
#user.do_some_action
... thus, when you have Registration, if that's the nature of the data you're storing, it's great. If it isn't, you'll be best suited changing its name to something like User or Applicant etc.
Lastly, you need to cut down on all the data you're storing in one model.
You can split your model into about 2 others:
#app/models/registration.rb
class Registration < ActiveRecord::Base
has_one :address
has_one :preference
accepts_nested_attributes_for :address, :preference
end
#app/models/address.rb
class Address < ActiveRecord::Base
belongs_to :registration
end
#app/models/preference.rb
class Preference < ActiveRecord::Base
belongs_to :registration
end
This will allow you to pass multiple sets of data, rather than keeping it all in the same model:
#app/controllers/registrations_controller.rb
class RegistrationsController < ApplicationController
def new
#registration = Registration.new
#registration.build_address
#registration.build_preference
end
end
#app/views/registrations/new.html.erb
<%= form_for #registration do |f| %>
<%= f.fields_for :address do |a| %>
<%= a.text_field :address %>
<% end %>
<% end %>
You can read up more about accepts_nested_attributes_for here.
not sure if you have missed some of your form code but there needs to be
<%= f.submit "Submit" %>
<% end %>
..also, your flash messages are working in the background but you need to create a place for them to show up. Go to your application.html.erb in your layouts folder and after the opening body tag add:
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "#{key}" %>
<% end %>