Conditional html in rails forms - a better way? - ruby-on-rails

I have a form with conditional bootstrap:
= simple_form_for(#user) do |f|
= f.input :first_name
- if f.object.new_record?
%button.btn.btn-primary{"data-target" => "#accordion", "data-toggle" => "collapse", :type => "button"}
.fa.fa-cog
More options
#accordion.collapse
= f.input :many_more_fields
- else
= f.input :many_more_fields
Is there a better way to make only the css conditional, in order not to have to duplicate = f.input :many_more_fields.
CSS part:
%button.btn.btn-primary{"data-target" => "#accordion", "data-toggle" => "collapse", :type => "button"}
.fa.fa-cog
More options
#accordion.collapse
P.S. Any ideas, except creating a partial for = f.input :many_more_fields?

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'

Ruby on Rails, testing for nil in second dimension of the params array

I'm having a bear with some very basic code, which is ...
.form
= semantic_form_for 'thought', :url => thoughtstep2_path do |f|
= f.inputs :name => 'Add Something' do
= f.input :title, :hint => "A Hint", :input_html => { :value => params[:thought][:title] }
= f.input :moreinfo, :as => "text", :hint => "Another Hint", :input_html => {:value => params[:thought][:moreinfo]}
= f.buttons
Because the params array I am using to set the :value has a second dimension it bugs out with
You have a nil object when you didn't expect it!
I've tried all sorts of get arounds but to no avail, any ideas anyone?
A few options to try:
{ :value => params[:thought].try(:[], :title) }
{ :value => (params[:thought][:title] rescue nil) }
{ :value => params[:thought] && params[:thought][:title] }
{ :value => (params[:thought][:title] if params[:thought]) }
I'd suggest that you pick the one that you personally find the most readable.
In your helper :
def test(value)
if value
return value;
else
return '';
end
end
In your view :
.form
= semantic_form_for 'thought', :url => thoughtstep2_path do |f|
= f.inputs :name => 'Add Something' do
= f.input :title, :hint => "A Hint", :input_html => { :value => test(params[:thought][:title]) }
= f.input :moreinfo, :as => "text", :hint => "Another Hint", :input_html => {:value => params[:thought][:moreinfo]}
= f.buttons
I'm not sure about this but it's may be a way to fix your problem

Rails 3 very long, very slow form

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

Resources