simple_form validation message for a field not in database - ruby-on-rails

class Customer < ApplicationRecord
attr_accessor :date
validates_presence_of :name, :principalAmount,:interestRate,:accountType,:duration,:date
end
Except date all are there in my customers table. I am using simple_form for getting those values. But problem is that there is no validation happening for :date.For others it displays message if the field is empty. How do I display presence_of validation message in simple_form for :date. I am using bootstrap datepicker.
<%= simple_form_for #customer do |f| %>
<%= f.input :name,:autocomplete => :off %>
<%= f.input :principalAmount,:autocomplete => :off %>
<%= f.input :interestRate %>
<%= f.input :accountType %>
<%= f.input :duration,:autocomplete => :off %>
<%= f.text_field :date, "data-provide" => 'datepicker',"data-date-format"=>"dd-mm-yyyy" %>
<%= f.button :submit %>
<% end %>
Edit: When I change f.text_field to f.input it shows the validation message. But it is no more a date picker.

Related

How do I add an autocomplete="street-address" attribute to rails form_for

I'm trying to add Mapbox autofill to my address bar on rails, for mapbox autofill to work I need to have autocomplete="street-address in an input field.
I'm using simple_form_form and I can't figure out how to add autocomplete attribute. Here is my form:
<%= simple_form_for #lock do |f| %>
<div class="form_wrapper">
<div class="form_div">
<%= f.input :name %>
<%= f.input :description %>
<mapbox-address-autofill>
<%= f.input :address, :autocomplete => "street-address" %>
</mapbox-address-autofill>
<%= f.input :photo, as: :file, label: "Link to picture" %>
<%= f.input :special_content %>
<%= f.button :submit, :class => "btn btn-primary" %>
<% end %>
any ideas on how to make the autocomplete attribute work?
Use input_html and pass the additional options like class etc.
<%= f.input :address, input_html: { :autocomplete => "street-address"} %>

Is it possible to create two objects from two forms in 1 view and have them related?

I know the title sounded a bit wonky, but this is what I am trying to do.
I have two models - Job and Company. Someone can create a Job listing, but before the listing is saved, it should be associated with a company. This company can be newly created, or should be populated from companies the current_user has previously created.
A job belongs_to :company, and a company has_many :jobs.
I know I could do two different views, and just send the user to two different actions on two different controllers, but I would like to simplify it and just have everything done in one view.
When they go to /jobs/new, they should see the normal jobs/_form.html.erb partial, but I would love to be able to show either a Company dropdown for existing companies or new company creation fields that the user fills out and that new company gets associated with this new job that is being created.
This is what my jobs/_form.html.erb partial looks like:
<%= simple_form_for(#job) do |f| %>
<%= f.input_field :title %>
<%= f.input_field :salary %>
<%= f.input_field :description, as: :text %>
<%= f.input_field :apply_description, as: :text %>
<%= f.input_field :language %>
<%= f.input :premium, as: :boolean, inline_label: true, label: "Make this listing stand out", class: "form-control" %>
<%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
<% end %>
This is what my companies/_form.html.erb looks like:
<%= simple_form_for(#company) do |f| %>
<%= f.input :name %>
<%= f.input :logo %>
<%= f.input :description %>
<%= f.input :city %>
<%= f.input :state %>
<%= f.input :country %>
<%= f.input :email %>
<%= f.button :submit %>
<% end %>
How can I combine them into 1 form, or some other unified workflow within 1 view where it works seamlessly to the user?
Edit 1
Based on Jay-Ar's answer, this is what is happening now.
When I select New Company in the Company Dropdown, it doesn't show the fields in the <fieldset>. I believe that's the case because there is no value=0 in the select tags rendered in the HTML, as can be seen in the screenshot below.
Edit 2
After attempting the latest update from Jay-Ar, the JS still doesn't work and the form is no longer hidden.
This is what it looks like on first load, and always:
Ideally I would like for this form not to show up until they have chosen "New Company" from the dropdown.
This is what the HTML looks like now:
The JS does appear in the source, so I know it is being loaded in the asset pipeline correctly.
UPDATED & TESTED WORKING
Now supports Turbolinks
views/jobs/_form.html.erb
<%= simple_form_for(#job) do |f| %>
<%# IMPORTANT: use `include_blank` below instead of `prompt` because prompt does not seem to work when updating, but only works when creating %>
<%= f.association :company, collection: [['New Company', nil]] + Company.pluck(:name, :id), include_blank: 'Please Select Company', input_html: { id: 'company-select' } %>
<fieldset id='job-fields'>
<%= f.simple_fields_for :company, #job.build_company do |ff| %>
<%= ff.input :name %>
<%= ff.input :logo %>
<%= ff.input :description %>
<%= ff.input :city %>
<%= ff.input :state %>
<%= ff.input :country %>
<%= ff.input :email %>
<% end %>
</fieldset>
<%= f.input_field :title %>
<%= f.input_field :salary %>
<%= f.input_field :description, as: :text %>
<%= f.input_field :apply_description, as: :text %>
<%= f.input_field :language %>
<%= f.input :premium, as: :boolean, inline_label: true, label: "Make this listing stand out", class: "form-control" %>
<%= f.button :submit, class: "btn btn-lg btn-primary pull-right" %>
<% end %>
JS
// for Rails 5, use turbolinks:load instead of page:change below
$(document).on('page:change', function(){
var companySelect = $('#company-select');
var jobFields = $('#job-fields');
companySelect.change(function(){
// if selected option is the second option
if ($(this).find('option:selected').index() == 1)
jobFields.show().find(':input').prop('disabled', false);
else
jobFields.hide().find(':input').prop('disabled', true);
})
// call change immediately so this still works when already updating and not just creating.
companySelect.change();
})
controllers/jobs_controller.rb
class JobsController < ApplicationController
...
def create
#job = Job.new(job_params)
...
end
def update
#job = Job.find(params[:id]) # not needed if using before_action #set_job
if #job.update(job_params)
...
end
private
def job_params
params.require(:job).permit(:id, :title, :salary, :description, :apply_description, :language, :premium, :company_id, company_attributes: [:name, :logo, :description, :city, :state, :country, :email]
end
end
models/job.rb
class Job < ActiveRecord::Base
accepts_nested_attributes_for :company
validates :company, presence: true
...
end
You should get something like the following:
On fresh load:
After selecting 'New Company' option':
You should use nested model forms, I suggest you watch this video it has a very detailed explanation of the steps to do it for a question / answer models but its the same thing.

Rails Form Object Returns Object ID in mail

I have the below form which works absolute fine but when submitted the :event field returns an ID in the mailer, any ideas how to prevent this?
Form
<%= simple_form_for #sponsorship_inquiry, :method => :post do |f| %>
<%= f.input :spam, as: :hidden %>
<%= f.input :name %>
<%= f.input :phone %>
<%= f.input :email %>
<%= f.input :job_title %>
<%= f.input :company %>
<%= f.input :event, :collection => Event.where(:end_date.gt => Date.today, :is_live => 'true') %>
<%= f.input :message, as: :text, :input_html => { :cols => 5, :rows => 6 } %>
<%= f.button :submit %>
<% end %>
Mailer
Name: <%= #sponsorship_inquiry.name %>
Phone: <%= #sponsorship_inquiry.name %>
E-Mail: <%= #sponsorship_inquiry.email %>
Job Title: <%= #sponsorship_inquiry.job_title %>
Company: <%= #sponsorship_inquiry.company %>
Event: <%= #sponsorship_inquiry.event %>
Message: <%= #sponsorship_inquiry.message %>
Controller
def new
#sponsorship_inquiry = SponsorshipInquiry.new
end
def create
# Hidden field for bots/spiders
redirect_to new_inquiry_path and return if params[:spam].present?
#sponsorship_inquiry = SponsorshipInquiry.new(params[:sponsorship_inquiry])
if #sponsorship_inquiry.valid?
SponsorshipInquiryMailer.admin(#sponsorship_inquiry).deliver
redirect_to sponsorship_inquiries_path
else
render :new
end
end
Need your SponsorshipInquiry model.
If you have
class SponsorshipInquiry < ActiveRecord::Base
belongs_to :event
end
try send <%= #sponsorship_inquiry.event.name %> or whatever )
Or you need to parse needed value from the form if "event" is only field not associated with Event model.
IMHO
If your question is "can I modify the form so that I automatically (magically?) get an object as a param?", the answer is definetly no.
What you have to do is to search the event object in the database based on the received id.

Add two values to a form select box

I want to include BOTH manager first name and last name in a select box below. How can I accomplish this?
Form:
<%= simple_form_for #office do |f| %>
<%= f.input :street_address %>
<%= f.input :city %>
<%= f.input :postal_code %>
<%= f.input :description, as: :text %>
<%=f.input :manager_id, collection: Manager.all, :id, :last_name, include_blank: true %>
<%= f.submit 'Add Office' %>
You can add something like
label_method: lambda { |manager| "#{manager.first_name} #{manager.last_name}" }
to your f.input
or you can create a new method "name" in your model, and use this one instead
def name
"#{first_name} #{last_name}"
end
then
label_method: :name

No errors and no output for rails cocoon gem

I am working on a dynamically nested form using the cocoon gem. I have two models
class CrossTable < ActiveRecord::Base
attr_accessible :title, :table_name, :database, :folder_label_id, :foreign_fields_attributes
belongs_to :folder_label
has_many :foreign_fields
accepts_nested_attributes_for :foreign_fields
validates :title, :table_name, :database, :folder_label_id, presence: true
end
class ForeignField < ActiveRecord::Base
attr_accessible :cross_table_id, :column_name, :description
belongs_to :cross_table
has_many :filter_sets
end
I have cocoon and jquery-rails in the gemfile
I added //=require cocoon to the application.js file
And here is my form partial
<%= simple_form_for #table do |f| %>
<%= f.input :title %>
<%= f.input :folder_label_id, :collection => #folders, :label_method => :title, :value_method => :id %>
<br><br>
<%= f.input :table_name %>
<%= f.input :database %>
<%= f.simple_fields_for :foreign_fields do |fields| %>
<%= render 'foreign_field_fields', :f => fields %>
<div id='links'>
<%= link_to_add_association 'Add Field', f, :foreign_fields %>
</div>
<% end %>
<%= f.button :submit %>
<% end %>
#table is an instance of the cross table model. Nothing in the foreign_field_fields partial shows up and link_to_add_association does nothing, and I get no errors. How can I start debugging this? Does anyone spot an error?
You wrote the link_to_add_association inside the simple_fields_for, which will loop over all :foreign_fields and execute the given block. So if there are no foreign-fields yet, the link_to_add_association is never shown.
You should write your view as follows (as documented):
<%= simple_form_for #table do |f| %>
<%= f.input :title %>
<%= f.input :folder_label_id, :collection => #folders, :label_method => :title, :value_method => :id %>
<br><br>
<%= f.input :table_name %>
<%= f.input :database %>
<%= f.simple_fields_for :foreign_fields do |fields| %>
<%= render 'foreign_field_fields', :f => fields %>
<% end %>
<div id='links'>
<%= link_to_add_association 'Add Field', f, :foreign_fields %>
</div>
<%= f.button :submit %>
<% end %>
Hope this helps.

Resources