New formtastic user here.
I have a relationship user has_many clients
In formtastic, if I do something such as
f.input :employer
it returns a select box of all employer object references. I'd like to display (last name, first name) instead. I'm sure this is very simple, but I can't figure out exactly how to do it.. Any help appreciated.
These didnt work for me, however, this did:
<%= f.input :user, :label => "Author", :label_method => :username %>
Also a little cleaner ^^
Or, you can set the display method once and for all on the model itself:
(In employer.rb)
def to_label
email
end
Try
f.input :employers, :as => :select, :collection => Employer.find(:all, :order => "last_name ASC")
or
f.input :employers, :as => :select, :collection => Employer.collect {|e| [e.last_name, e.id] }
Related
I have the following Rails view/form association:
<%= f.association :workorder, :collection => Workorder.wonotclosed.order(:description), :label_method => :description, :label => 'Project' %>
The issue is the workorders are sorted using uppercase first and then lowercase. I would like it to be case insensitive.
I tried these:
:collection => Workorder.wonotclosed.order(LOWER(description))
:collection => Workorder.wonotclosed.order(:description.downcase)
Thanks for the help!
You need to pass the SQL snippet in as a string:
Workorder.wonotclosed.order("LOWER(description)")
In my Rails application, I have the following model:
class Idea < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :ideas
end
I am creating ActiveAdmin CRUD for my Idea model with the custom form that looks something like that looks something like that:
form do |f|
f.inputs do
f.input :member
f.input :description
end
end
The requirement is to have the custom text for a content of the member association, i.e "#{last_name}, #{first_name}". Is it possible to customize my member select box to achieve it?
Any help will be appreciated.
Yes, that is possible. I assume you want to use a DropDown list box for members to select a user from User model.
form do |f|
f.inputs do
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}
f.input :description
end
end
For Active Admin You have to pass it as a collection of Hashes. Key in hash will be the text which you want to display and value will be the attribute id.
For Example:
f.input :user_id, :label => 'Member', :as => :select, :collection => User.all.map{|u| ["#{u.last_name}, #{u.first_name}", u.id]}.to_h
collection: [{name1: 1}, {name2: 2}, {name3: 3}]
Note: I have added to_h at end of the map which will convert a collection of arrays into a collection of hashes.
I'm new to rails and I guess you can answer this question easily.
What I got so far is
= f.input :task, :as => :select, :collection => #tasks, :include_blank => true
where the tasks collection is defined by
Task.find(:all)
within in the controller.
This does in fact work, shows me a dropdown-list of all Tasks to select from and connects them.
The problem here is, that the dropdown menu shows me values like
#<Task:0x123456789d1234>
Where do I define what value is being displayed?
I believe you can use the :label_method to solve your problem...
f.input :task, :as => :select, :collection => #tasks,
:include_blank => true, :label_method => :title
where :title is what you want to show.
This may help a little more.
You can define a to_s method in the model:
class Task < ActiveRecord::Base
def to_s
title # the attribute to display for the label
end
end
I use the superb Formtastic plugin for Ruby on Rails.
Does anybody know how to include a blank (option), when using a custom collection?
When I try:
<%= f.input :organizations, :collection => Organization.all(:order => :name), :include_blank => true %>
I get the select box with the collection, but NOT a blank...
What kind of association is :organizations? Does it work if you specify :as => :select?
There's spec coverage for the following belongs_to select, date, time and datetime inputs:
f.input(:author, :as => :select, :include_blank => true)
f.input(:created_at, :as => :date, :include_blank => true)
f.input(:created_at, :as => :time, :include_blank => true)
f.input(:created_at, :as => :datetime, :include_blank => true)
My guess is that organizations is not a belongs_to association, right? If it's a :has_many or :has_and_belongs_to_many association, Formtastic will try to do checkboxes or a multi-select. In the case of a multi-select, obviously it makes no sense to have a blank line in there (you just don't select any of the items).
Hope this helps, please post some more details about the models and associations in question.
How do I instruct Formtastic select to only display values based on a condition?
- semantic_form_for #member do |f|
- f.inputs do
= f.input :person
= f.input :role, :include_blank => false
= f.input :active
I only want the :person input to list/select people who are active i.e. person.active == true. I tried to pass a condition hash map to no avail.
This is a two step process.
First you need a means of selecting only People that are active. Next you need to pass that collection of active people to the formtastic input via the :collection option.
Step one selecting only active people: This is as simple as Person.find(:all, :conditions ["active = ?", true]). But I think this is better accomplished with a named scope in the model.
class Person < ActiveRecord::Base
# ...
named_scope :active, :conditions => {:active => true}
end
Now Person.active is the same as Person.find(:all, :conditions ["active = ?", true])
Step two, updating the form:
- semantic_form_for #member do |f|
- f.inputs do
= f.input :person, :collection => Person.active
= f.input :role, :include_blank => false
= f.input :active
You can supply any custom collection of values through the :collection option:
f.input :person, :collection => Person.find(:all, :conditions => "whatever")