Rails 3 very long, very slow form - ruby-on-rails

I have a very long form with nested form fields. It takes 30 seconds in development to load.
I've used a separate model for each drop down menu that I need - I don't know if that's such a good idea now that I have 35 of them. I also have 11 accepts_nested_attributes_for/ cocoon gem partials, which is super once the page has loaded, but I suspect the 11 different partials are slowing things down.
I'm caching the page which helps, and in production it's a lot quicker - 8 seconds - but still not really acceptable.
How should I ideally structure this long form? I can't think it's RESTful to split up an edit / update method. Or if the structure is OK, what can I do to improve performance? Like I say I'm caching, and there are no finds in the view. The controller is pretty regular although there's lots of calculations in methods in the model.
Here's a fragment. Many thanks in advance for any thoughts.
%a{:name => "Measurements"}
- f.inputs :name => "Measurements", :id => "main" do
%li.tip
= tooltip(:descriptivedetail_productcomposition_productcomposition, :hover)
= f.input :descriptivedetail_productcomposition_productcomposition, :label => "Product composition", :as => :select, :collection => Productcomp.all, :value_method => :code
%li.list
= link_to "Edit list", productcomps_path
%br
%li.tip
= tooltip(:descriptivedetail_productcomposition_productform, :hover)
= f.input :descriptivedetail_productcomposition_productform, :label => "Product form", :as => :select, :collection => Productform.all, :value_method => :code
%li.list
= link_to "Edit list", productforms_path
%br
%li.tip
= tooltip(:descriptivedetail_primarycontenttype, :hover)
= f.input :descriptivedetail_primarycontenttype, :label => "Content type", :as => :select, :collection => Contenttype.all, :value_method => :code
%li.list
= link_to "Edit list", contenttypes_path
%br
%li.tip
= link_to_add_association 'Add measurement', f, :measurements
= f.semantic_fields_for :measurements do |measurement|
= render 'measurement_fields', :f => measurement
%br
%li.tip
= link_to_add_association 'Add extent', f, :extents
= f.semantic_fields_for :extents do |extent|
= render 'extent_fields', :f => extent
%br
%a{:name => "Supply"}
- f.inputs :name => "Supply", :id => "main" do
%li.tip
= link_to_add_association 'Add supply info', f, :supplies
= f.semantic_fields_for :supplies do |supply|
= render 'supply_fields', :f => supply
%br
%a{:name => "Rights"}
- f.inputs :name => "Rights", :id => "main" do
%li.tip
= link_to_add_association 'Add rights info', f, :rights
= f.semantic_fields_for :rights do |right|
= render 'right_fields', :f => right
%br
%a{:name => "Related"}
- f.inputs :name => "Related", :id => "main" do
%li.tip
= link_to_add_association 'Add related product info', f, :relatedproducts
= f.semantic_fields_for :relatedproducts do |relatedproduct|
= render 'relatedproduct_fields', :f => relatedproduct
%br
An example partial:
.nested-fields
= f.inputs do
%br
%li.tip
= tooltip(:descriptivedetail_measure_measuretype, :hover)
= f.input :descriptivedetail_measure_measuretype, :label => "Measure type", :as => :select, :collection => Measuretype.all, :value_method => :code
%li.list
= link_to "Edit list", measuretypes_path
%br
%li.tip
= tooltip(:descriptivedetail_measure_measurement, :hover)
= f.input :descriptivedetail_measure_measurement, :label => "Measurement"
%li.tip
.links
%br
%li.tip
= tooltip(:descriptivedetail_measure_measureunitcode, :hover)
= f.input :descriptivedetail_measure_measureunitcode, :label => "Unit", :as => :select, :collection => Measureunit.all, :value_method => :code
%li.list
= link_to "Edit list", measureunits_path
%br
= link_to_remove_association "Remove measurement", f

Related

Label option for collection_select in rails

I have applied localization for a select box label as follows. Label option not working in collection_select in rails form. How can I change the code to get the label
Updated
= simple_form_for #product do |f|
= error_notification f
.form-inputs
.row
.span5
= f.input :name, :input_html => {:maxlength => 100}
.span5
- unless #product.company.nil?
= f.input :company_id, :as => :hidden, :input_html => { :value => #product.company.id}
- else
= f.association :company, :prompt => "Select Company"
.row
.span10
%div#product_existence_message
- unless #product.company.nil?
= f.collection_select :product_type_id, ProductType.all,:id, :name, {:prompt => 'Select Product Type', :label => :label => t('forms.products.label.name'), :selected =>#product.product_type_id }
File: en.yml
en:
activerecord:
attributes:
product:
product_type: 'Product Type translation here'
File: template.html.erb
<%= label(:product, product_type) %>
# => <label for="product_product_type">Product Type translation here</label>
Reference: https://apidock.com/rails/ActionView/Helpers/FormHelper/label[https://apidock.com/rails/ActionView/Helpers/FormHelper/label]

Simple_Form: Make two forms work with eachother (dropdown and search)

I have two forms, one containing a dropdown where the user can choose how a list is beeing sorted, the other one containing a searchfield, where the user can search through that list. Now if a user searches for "test" and ten results show up, I want the user to be able to choose from the dropdown, how the results are beeing sorted. Accordingly if he sorts the whole list, I want himto be able to search through the list, with the results showing up in the sorted way he choose before. Due to code restrictions I have to keep those two inputs in different forms.
Here is the sort-dropdown:
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
And here is the search:
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
Is it possible to keep the two inputs seperate or would it be way easier to use just one form?
You can use separate forms if you need to, just store the other parameter in a hidden field in each of the forms.
= simple_form_for path, :method => "get", html: {id: "sortform"} do |f|
= f.input :sort, :collection => [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :label => false, :required => false, :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')
= f.input :q, as: :hidden, input_html: { :name => :q, :value => params[:q] }
= simple_form_for path, :method => 'get', :label => t(:'videos.search'), html: {id: "search-form"} do |f|
= f.input :q, { input_html: { class: 'form-control searchbar', :name => :q, id: "search", :value => params[:q]}, :placeholder => t(:'videos.search'), :required => false, :label => false}
= f.input :sort, as: :hidden, collection: [t(:'videos.date'), t(:'videos.title'), t(:'videos.length')], :selected => params[:sort], input_html: {class: "control", :id => "sort_dropdown", :name => :sort}, :include_blank => t(:'videos.sort')

Hidden values show up as blank

I have a form using rails and being submitted by angularjs. The problem I'm having is that when I try to access the form data from angular, the input that is not hidden will show up in the form newTask object, but the fields that are hidden won't. Is there something I am missing here?
= form_for [#patient, Task.new], html: {action: nil, name: 'newTaskForm', 'ng-submit' => 'taskCreate($event, newTask)'} do |f|
= f.hidden_field :taskable_id, :value => #patient.id,
'ng-model' => 'newTask.taskable_id'
= f.hidden_field :taskable_type, :value => #patient.class.to_s,
'ng-model' => 'newTask.taskable_type'
= f.hidden_field :creator_id, :value => current_user.id,
'ng-model' => 'newTask.creator_id'
= f.text_field :text, required: true, placeholder: 'Add a task...',
'ng-model' => 'newTask.text'
= f.submit 'Add task', 'ng-disabled' => 'newTaskForm.$invalid'

Rails 3: Text_field_tag default value if params[:something].blank?

How would one do something like:
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:age].blank? or "20"
Above doesen't work. I want to be able to set a default value if there is no param available for that attribute.
Any smart way to do this thx!
EDIT 1:
Full form:
= simple_form_for :people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age',
:selected => params[:people][:age_from] || "20"
= f.submit "Go »"
You're using helpers that are taking their values from the object you're building the form on.
So in your controller, you should preset the values on the object.
def some_action
#people = People.new
#people.age = params[:age] || 20
end
Then in the form, remove the :selected option and it should be fine. Make sure you build the form for #people :
= simple_form_for #people, :url => request.fullpath, :method => :get, :html => { :class => 'form-search' } do |f|
#container_search_small.form-search
= f.input_field :age,
:collection => 18..60,
:id => 'age'
= f.submit "Go »"

Formtastic prints a number in nested form

I have this form
- semantic_form_for :qualification, :url => { :controller => 'qualifications', :action => 'add_students' } do |form|
- form.inputs do
= form.input :name, :label => "Course Title"
= form.input :course_type, :collection => Qualification::COURSE_TYPES
= form.input :graduate_date, :label => "Course Start Date", :start_year => pdc_start_year, :end_year => 1972, :discard_day => true
= form.input :location, :label => "Course Location"
= form.semantic_fields_for :user do |student|
= student.input :first_name
= student.input :last_name
= form.submit 'Add Students'
and it creates before the button this
1
I see this 1 generated but I would like to remove it
Just guessing here that you're using rails 2?
= form.semantic_fields_for :user do |student|
should be
- form.semantic_fields_for :user do |student|

Resources