simple_fields in Rails - ruby-on-rails

I have a simple_field and I pass an object to a partial file. I do this in the partial :
<%= f.input :address,
:as => :hidden,
:label => "Address :",
:input_html=>{
:required => false,
:class => "address"
} %>
I want to use :as => hidden depending on a certain condition. I mean the field should be hidden if a condition is true. Is that possible to do?

You could make your options hash separately and then add the hidden parameter if necessary
options = {
:label => "Address :",
:input_html=>{
:required => false,
:class => "address"
}
}
if condition_is_true? then options[:as] = "hidden" end
<%= f.input :address, options %>

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]

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)

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

Resources