Use hash as source for collection_check_boxes - ruby-on-rails

I have the following hash:
FIELD_LIST = {
-1 => 'User',
-2 => 'Duration',
-3 => 'Price',
-4 => 'Invoiced'
}
I want to use this with a collection_check_boxes.
In the manual it says:
The :value_method and :text_method parameters are methods to be called on each member of collection.
So I tried this:
= f.collection_check_boxes TimesheetReport::FIELD_LIST, [0], [1], :input_html => { :class => 'checkbox' }
But that gives me an error.
How is it possible to use a hash as input to generate the checkboxes?

Actually, it is possible. A Hash is technically a collection of objects. You can do something like this:
= f.collection_check_boxes :field_name, TimesheetReport::FIELD_LIST, :first, :last, :input_html => { :class => 'checkbox' }
Replace :field_name with the actual name of your attribute where you want to store this data. It should work.

Related

simple_fields in 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 %>

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

Delete a rails relationship with an :include_blank

I want to be able to select a blank object and post it so that I can clear a rails relationship. By default when you POST while selected on the :include_blank entry, nothing gets posted so it doesn't delete the old relationship. So I'm trying to add a 0 id blank item to the array.
Original:
<%= select_f f,:config_template_id, #operatingsystem.config_templates.where(:template_kind_id => f.object.template_kind_id), :id, :name, {:include_blank => true}, { :label => f.object.template_kind } %>
Mine:
<%= select_f f,:config_template_id, #operatingsystem.config_templates.where(:template_kind_id => f.object.template_kind_id).collect { |c| [c.name, c.id]}.insert(0, ['', 0]) %>
I'm getting a "wrong number of arguments (3 for 5)" error though and can't figure out what I'm missing. Any pointers? (I also can't find select_f anywhere on web, I think google ignores the _ so the search is way to open ended... For rails 3 should I be using something else?)
You've omitted the 3rd, 4th, 5th, and 6th argument you were passing into your original code block. Whatever select_f is, it's expecting at least 5 arguments. In the original you pass the following to select_f (one argument per line for clarity)
f,
:config_template_id,
#operatingsystem.config_templates.where(:template_kind_id => f.object.template_kind_id),
:id,
:name,
{:include_blank => true},
{ :label => f.object.template_kind }
In your new (broken) call you're only passing
f,
:config_template_id,
#operatingsystem.config_templates.where(:template_kind_id => f.object.template_kind_id).collect { |c| [c.name, c.id]}.insert(0, ['', 0])
Use your first method call, just replace the 3rd argument.
f,
:config_template_id,
#operatingsystem.config_templates.where(:template_kind_id => f.object.template_kind_id).collect { |c| [c.name, c.id]}.insert(0, ['', 0])
:id,
:name,
{:include_blank => true},
{ :label => f.object.template_kind }
Finally, if you don't want the :include_blank => true to be passed, but still do want the label, just pass nil or {} to the 5th argument
f,
:config_template_id,
#operatingsystem.config_templates.where(:template_kind_id => f.object.template_kind_id).collect { |c| [c.name, c.id]}.insert(0, ['', 0])
:id,
:name,
nil,
{ :label => f.object.template_kind }
And altogether on one line:
<%= select_f f, :config_template_id, #operatingsystem.config_templates.where(:template_kind_id => f.object.template_kind_id).collect { |c| [c.name, c.id]}.insert(0, ['', 0]), :id, :name, nil, { :label => f.object.template_kind } %>
I can't guarantee this works because I don't know where the API for select_f is or if you created it yourself. This should however nudge you in the right direction.

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 »"

Where to put a class on the f.select in Ruby?

I am trying to apply a class to a select form element that is built with Ruby.
Here is the f.select:
<%= f.select :within, ["5km", "50km", "100km", "250km", "500km", "1000km"], { :include_blank => 'Anywhere', :selected => (params[:listing][:within] unless params[:listing].nil? || !params[:listing].has_key?(:within)) } %>
Where does the class go? My class is:
:class => 'input-medium'
Thanks!
See the documentation, specifically the html_options argument.
Add yet another argument onto the end of what you've posted in your question with:
{ class: "input-medium" }
Altogether that's
<%= f.select :within, ["5km", "50km", "100km", "250km", "500km", "1000km"], { :include_blank => 'Anywhere', :selected => (params[:listing][:within] unless params[:listing].nil? || !params[:listing].has_key?(:within)) }, { class: "input-medium" } %>

Resources