Label option for collection_select in rails - ruby-on-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]

Related

Conditional html in rails forms - a better way?

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?

How to add custom name input field in rails simple_form_for

#tech_data = TagNode.first outputs below object from the controller .
p #tech_data
#<TagNode _id: 5119dcf74054448e576f3392, parent_id: nil, parent_ids: [], name: "categories", path: "categories", _type: "TagNode">
I have a form which has the fields name, _type, and category. But my object doesn't have the field category.
Here's my form
= simple_form_for #tech_data, as: :techtags, :url => view_techtags_technologies_path, :remote => true,:method => :get,:html => {:id => "upsert_techtags",:data => {:spinner => "#tech-ui"}} do |f|
%label.pull-left Type
= f.input :_type, :collection => tech_types, :label => false, :input_html => {:class =>"chzn-select", :data => {:placeholder => "Select Technology Type"}, :multiple => false}
%label.pull-left Name
= f.input :name, :type=>"text", :required => true, :label => false, :placeholder =>"Type the New Technology", :input_html => {:style => "width:265px;margin-bottom:0;"}
%label.pull-left Category
= f.input :category, :collection => [1,2,3,4], :label => false, :input_html => {:class =>"chzn-select", :disabled=>"true",:data => {:placeholder => "Type or select category"}, :multiple => false}
= f.submit "Create", :class => "btn btn-primary pull-right"
My form has an additional field that my object doesn't have. I get an error when the form is loaded, as I don't have category field in my object.
How can I select the category value in the form without adding this field to the model?
You need something like attr_accessor in Ruby,What is attr_accessor in Ruby?
Please include following in your model,which will make to get values from form to controller.
attr_accessor :category

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')

Rails 3.2 validating string inclusion default value not detected

I am using mongoid with a model field and given validation:
field :status, type:String, :default=>'Active'
validates :status, :inclusion=>{:in=>%w(Active, Done, Canceled, Merged)}, :allow_nil=>true, :allow_blank=>true
in the form, I do not have the status field, so it's supposed to be not POST-ed therefore it's nil on creation:
= simple_form_for([#user, #task], :html => {:class=>'form-horizontal',:'data-type'=>'html'}) do |f|
- if #task.errors.any?
.error_explanation
.alert.alert-error
The form contains
= pluralize(#task.errors.count, 'error')
%ul
- #task.errors.full_messages.each do |msg|
%li=msg
.form-inputs
= f.error_notification
= f.association :project, :collection => current_user.projects.all
= f.input :description, :as => :text, :input_html => {:rows => 5}
= f.input :priority, :as=>:radio_buttons, :collection=>1..4, :item_wrapper_class=>'inline'
= f.input :due_date
.control_group.select.optional
= f.label :assigned_to, :class=>'select optional control-label', :for => 'assigned_to_id'
.controls
= f.collection_select :assigned_to_id, User.all, :id, :username, :class => 'select optional'
.form-actions
= f.button :submit, :class => 'form-button btn-primary', 'data-loading-text' => 'Submitting...'
however, I am still getting this despite setting a default value "Active", which is obviously in the array provided for the validation of inclusion:
Status is not included in the list
why am I still getting this error?
Thanks in advance!
This is your issue
%w(Active, Done, Canceled, Merged)
which translates to
["Active,", "Done,", "Canceled,", "Merged"]
solution is to remove the commas
%w(Active Done Canceled Merged)

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