Formtastic Collection Select Dropdown - ruby-on-rails-3

When using formtastic in my rails3 application, I can't get the dropdown menus to work. Instead they appear as a selection list.
My code looks like this:
<%= f.label :slas, "Service" %></td><td><%= f.input :slas, :collection => #slas %>
Is this something I need to change in the css?

Wow, that took some fiddling.
In order to get a single dropdown list, instead of the select box, I needed to do this:
<%= f.label :slas, "Service" %></td><td><%= f.input :slas, :as => :select, :multiple => false, :input_html => { :size => 1 } %>
That was surprisingly hard...

That's strange. :as => :select should have done the trick by itself.

Related

How to show two 'f.input' in one line in activeadmin form page

form do |f|
f.inputs :question do
f.input :id, :as => :hidden
f.input :questionaire_id, :as => :hidden
f.input :role, :as => :hidden
f.input :question_type
f.input :description
f.input :option
f.input :score
end
f.actions
end
In above form, how to show this two inputs
f.input :option
f.input :score
in one line? Any idea?
I think I got the answer
First we should check this Formtastic::Helpers::InputHelper
According to the helper:
input is used to render all content (labels, form widgets, error messages, hints, etc) for a single form input (or field), usually representing a single method or attribute on the form's object or model.
The content is wrapped in an li tag, so it's usually called inside an inputs block (which renders an ol inside a fieldset).
It's option :wrapper_html can be used to override or add to the HTML attributes to be passed down to the wrapping li tag
So in the form we should do this:
form do |f|
f.inputs :question do
f.input :id, :as => :hidden
f.input :questionaire_id, :as => :hidden
f.input :role, :as => :hidden
f.input :question_type
f.input :description
f.input :option, :wrapper_html => { :class => 'fl' }
f.input :score, :wrapper_html => { :class => 'fl' }, :label => false
end
f.actions
end
:label => false can disabled the label of form input.
And then specify the class 'fl' in the css file(I just add following css from Include two inputs in same LI element in Formtastic in active_admin.css.scss like #Andrey Deineko told):
form.formtastic fieldset ol li.fl {display:inline;}
Then we have two inputs on the same line~Hope this will be helpful~

Rails simple_form grouped collection not working

In my Rails app, I'm using simple_form.
I'm trying to use grouped collection selects.
Without simple_form, this works:
<%= f.label :employee_id, "Lead3:" %>
<%= f.grouped_collection_select :employee_id, Workgroup.order(:id) , :employees, :group, :id, :employee_full_name %>
But, my simple_form attempt doesn't - the dropdown is empty:
<%= f.input :employee, collection: #workgroups, as: :grouped_select, group_method: :employees, :label => 'Lead2:' %>
OR
<%= f.association :employee, collection: #workgroups, as: :grouped_select, group_method: :employees, :label => 'Lead2:' %>
I would suggest to check your #workgroups definition in the controller. It probably returns nil or is not specified. Depending on what action calls your form, you should have something like this:
#workgroups = Workgroup.all(order: id)

Memory (hex) address being returned instead of value in Rails

I'm definitely a bit of a noob, so this might be something simple that I'm overlooking, however, the searches that I've done to try and find a solution have come up empty.
I've built a form using formtastic that has 5 input fields: two are text boxes and three are select lists.
<%= semantic_form_for #player do |f| %>
<%= f.inputs do %>
<%= f.input :firstname, :label => "First Name " %>
<%= f.input :lastname, :label => "Last Name " %>
<%= f.input :leagueid, :as => :select, :collection => League.all(:order => :leaguename), :label => "League " %>
<%= f.input :team_1, :as => :select, :collection => Team.all(:order => :name), :label => "Team 1 " %>
<%= f.input :team_2, :as => :select, :collection => Team.all(:order => :name), :label => "Team 1 " %>
<% end %>
<%= f.actions %>
<% end %>
What is happening is that the Teams lists work perfectly (the team names are displayed). However, the League list is a different story. All of the entries in the list look like this (with different a different code after 'League:'):
#<League:0x007fe29c406498>
If I use the form to create a Player, it works fine. The correct league ID goes into the database and everything. I just can't figure out why the names of the teams show, while whatever-that-is shows for the league.
Any and all help is appreciated.
When converting objects to String, Ruby will convert them to the memory address like you see unless you provide a to_s method for string conversions. I haven't used formtastic, but I believe adding a to_s method to your League class should cause it to display what you want.
Try adding
def to_s
#name # use whatever you want to be displayed.
end
to the League class.
You could try explicitly specifying the fields that should be used as the text and id within the select list. I believe it would look like.
<%= f.input :leagueid, :as => :select, :collection => Hash[League.all.map { |league| [league.leaguename, league.id] }]
The syntax is crazy. The call to map is returning an array of name/id pairs, like [ ['league1', 1], ['league2', 2] ]. Calling Hash on that converts it to a hash, like {'league1' => 1, 'league2' => 2}. Seems like the select list should use this hash to populate itself.
There's an example of this at http://rdoc.info/github/justinfrench/formtastic, under the Usage section.
:member_name is the solution I think.
<%= f.input :leagueid, :as => :select, :collection => League.all(:order => :leaguename), :label => "League " %>
Will probably work for you as
<%= f.input :leagueid, :as => :select, :member_name => :league, :collection => League.all(:order => :leaguename), :label => "League " %>
My problem was I have a model with a field the same name as the model and I think that confused Formtastic
Example scaffold:
rails g scaffold Countries code:string country:string
rails g scaffold Types title:string description:string
Model:
class Sign < ActiveRecord::Base
attr_accessible :title, :country_id, :type_id
belongs_to :country
belongs_to :type
Form View:
<%= f.input :type %>
<%= f.input :country, :member_label => :country %>
Having a form without the member_label leads to the object id displaying in the select box for countries although the ID is correctly saved. The type select worked perfectly without declaring member_label.
Note that I didn't need to specify :as => :select as formtastic can deduce this from the belongs_to relationship.
Try using :
<%= form.input :league, :member_label => :leaguename %>
This will override the naming convention of the Formtastic Column Select.
Have a look also at : Overriding the Column Name Convention wiki
Hope this helped.

Default value on select field in formtastic form with no model

I have a formtastic form to gather parameters for report generation. I know formtastic is designed to be used with models but I need to use it here as the form is in an activeadmin page.
It's all working well but I can't set a default value for the selects. I'm willing to implement a "despicable hack" to get this working. I'd prefer not to implement a fake model
just to set default values on a form.
The form code looks like this
<%= semantic_form_for "report", :url => report_admin_payments_path do |f| %>
<%= f.inputs do %>
<%= f.input :report_type, :as => :select, :collection => report_types, :input_html => {:style => "width:180px"} %>
<%= f.input :start_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => 1.month.ago.strftime("%Y-%m-%d")} %>
<%= f.input :end_date, :as => :string, :input_html => {:class => 'datepicker', :style => "width:60px", :value => Time.zone.now.strftime("%Y-%m-%d")} %>
<%= f.input :country, :as => :select, :collection => locales, :input_html => {:style => "width:180px"}%>
<% end %>
<%= f.actions :submit %>
<% end %>
Thanks in advance,
Matt
This or something similar should meet your needs.
class LightModel
# Subclass this for a model that looks like an ar model but has no persistence
# good for formtastic forms
include ActiveModel::Naming
include ActiveModel::Validations
def initialize(attributes = {})
#attributes = attributes
end
# read only atm
def method_missing(m, *args, &block)
#attributes[m]
end
end
Thanks,
Matt

Rendering the Devise edit Password Form

I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.
I've tried the following (after generating the Devise views):
<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>
And a number of other variations on render that all seem to give me the same error:
"ActionView::MissingTemplate in foo#foo
Missing partial devise/passwords/edit..."
This variation:
<%= render :file => 'devise/passwords/edit.html.erb' %>
Gave me some hope but the following error:
"undefined local variable or method `resource' for #<#:0x47ef0e0>"
around this line:
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?
Am I way off?
Try this:
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
Where:
my_user_model_variable could be current_user
my_user_model_name could be "User"