I have a code table called priorities. The table has a boolean called archive. I have a request table and it's form contains a pick list of priorities. I'm using scope statements to tell if a priority is archived or not. This is the code in the model:
scope :archived, where(:archive => true)
scope :active, where(:archive => false)
I'm trying to use the scope in the request form like this:
<%= f.association :priority, :label_method => :prioritycode.active, :label => 'Priority' %>
But, that doesn't work.
What should I be using in the form?
Thanks!
I may be missing something -- not familiar with the association helper you're using.
If you want a drop-down select list, I would do something like
<%= f.collection_select :priority, Priority.active, :id, :name %>
(Assumes you have a model Priority in which the scope :active is defined, and that a priority has a name. Your mileage may vary.)
tharrison post got me thinking (thanks) - this worked:
<%= f.association :priority, :collection => Priority.active.all, :label_method => :prioritycode, :label => 'Priority' %>
Related
How would one capitalise the first letter of a list of country's in below code example?
My question is regarding simple_form, not a regular form for which one could use collection_select.
= p.input :state,
:readonly => "readonly",
:collection => DataState.where(:country => "1"),
:selected => 1,
:id => "state",
:name => "state",
:prompt => "Please choose"
The problem is the list of states is in the database and saved like "alamaba","new york"
I need it to be down cased in the database but would like to uppercase/capitalize the first letter for visual clarity.
Try this:
In your DataState model, add a method that displays a capitalized version of your state names
class DataState < ActiveRecord::Base
def capitalized_state
state.capitalize
end
end
then use this new method to list your choices
Depending on if you're trying to save the id of the state as an association or just the string, the erb would be either
<%= f.input :state, collection: DataState.where(:country => "1").pluck(:capitalized_state) %>
or
<%= f.input :state, collection: DataState.where(:country => "1"), value_method: :id, label_method: :capitalized_state %>
where the former is for the state name and the latter is for the association id.
see this: using capitalize on a collection_select
Edit: I noticed that your html does not specify what attribute of the DataState class you're using as the text for your choices. If you're using rails, you should look into the collection_select form helper.
One way of going about it, too, would be the addition of the text-transform: uppercase property of css, looking like this:
<% = f.input: state, collection: DataState.where (: country => "1"), style: 'text-transform: uppercase;' %>
I am creating an input form for a Post model with Simple Form on a Rails app. The Post model is associated to a Keyword model with a has_and_belongs_to_many. To fill up the Tags in the form, I am using:
<%= f.association :keywords, collection: Keyword.all(order: 'name'), prompt: "Select keyword..." %>
which creates a html :select tag for the input. The problem is, because is a many to many association, Simple Form assigns the :multiple tag to :select by default, allowing selecting many objects. But I do want to force it to output a simple <select> with no multiple for this field.
Any idea how to do this? Thanks a lot!
Figured it out, pass :input_html => { :multiple => false }
<%= f.association :keywords, collection: Keyword.all(order: 'name'), prompt: "Select keyword...", :input_html => { :multiple => false } %>
I've got 3 models that comprise a has-many-through association.
Model code as follows:
ItemAttrVal Model (the transition table)
class ItemAttrVal < ActiveRecord::Base
belongs_to :attr_name
belongs_to :registry_item
end
RegistryItem Model
class RegistryItem < ActiveRecord::Base
has_many :item_attr_vals
has_many :attr_names, :through => :item_attr_vals
accepts_nested_attributes_for :item_attr_vals, :allow_destroy => :true
end
AttrName Model
class AttrName < ActiveRecord::Base
has_many :item_attr_vals
has_many :registry_items, :through => :item_attr_vals
end
The RegistryItem uses a fields_for as follows:
<%= item.fields_for :item_attr_vals do |iav| %>
<%= render 'item_attr_val_fields', :f => iav %>
<% end %>
In the partial, it looks like this:
<% logger.debug "object type is: #{f.object}"%>
<% logger.debug "some details are: #{f.object.attr_name_id}--"%>
<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description"), :selected => f.object.attr_name_id, :prompt => "Select an attribute" %>
<%= f.text_field :raw_value %> <br />
The 1st 2 debug lines are the bit that my question is about, but it first relates to the 3rd line.
There, I am attempting to provide the dropdown select field with a "pre-selected" value. This is so that when the user is editing the RegistryItem, their previously selected AttrName will be displayed.
I'm attempting to use the f.object.attr_name_id to set that value, however it does not actually properly select the previously selected value, and instead, just goes to the 1st.
The 1st two debug lines were then me trying to make sure that my f.object method worked...
When I looked in my logs, I see the following:
object type is: #<ItemAttrVal:0x007fb3ba2bd980>
some details are: --
Basically, the 1st line shows me that I am getting the ItemAttrVal
The second line does not seem to retrieve any information for it.
I've also used the debugger to check, and in there, I am able to use display f.object.attr_name_id to show me the exact value that I'm expecting...
This kind of comes down to two questions...
Why can't I retrieve the values of f.object?
Am I trying to do line 3 (<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description"), :selected => f.object.attr_name_id, :prompt => "Select an attribute" %>) wrong, and there's actually a better way to do it?
Thanks in advance!
you need to use params[:attr_name_id] into your options_from_collection_for_select
<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description", params[:attr_name_id].to_i), :prompt => "Select an attribute" %>
hope it helps
Turns out I'd placed the :selected in the wrong location...
Original:
<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description"), :selected => f.object.attr_name_id, :prompt => "Select an attribute" %>
Should be:
<%= f.select :attr_name_id, options_from_collection_for_select(AttrName.all,"id","description", f.object.attr_name_id), :prompt => "Select an attribute" %>
Fixing that solved my problem, the attribute names are now appearing as expected for previously saved attributes.
It still doesn't answer my original query about why I'm not able to get the values for f.object printed out, but at least the original-original problem was resolved.
I have this piece of code, while using simple_form:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes
How can I preselect a specific preset checkbox, knowing that the ID of this preset is passed within params[:preset_id]? The checkboxes' HTML name attributes are report[preset_ids][].
According to the simple_form documentation:
The association helper just invokes
input under the hood, so all options
available to :select, :radio and
:check_boxes are also available to
association. Additionally, you can
specify the collection by hand, all
together with the prompt:
f.association :company, :collection
=> Company.active.all(:order => 'name'), :prompt => "Choose a Company"
So, you should use something like this:
= simple_form_for :report do |f|
= f.association :presets,
:collection => #account.presets.collect{ |p| [p.name, p.id] },
:as => :check_boxes,
:checked => params[:preset_id]
I don't have experience with simple_form, but this might help :)
An update for everybody. the :selected option did not work for me. I used:
:checked => [2, 3]
Hope it helps someone.
f.association Really did the trick, thanks :), for preselecting, saving, and everything, I don't have reputation enough to vote up your answer (#claudio-acciaresi) that's why I'm commenting here...
This is my snippet:
<%= f.association :association, collection: Model.all,
value_method: :id, label_method: :name,
as: :check_boxes, include_blank: false %>
Replace symbol :association with the current has_many from the model.
Replace Model.all, for your source data.
Hope it gets useful for someone else :)
Regards.
Don't forget to cast params[:preset_id] to integer:
params[:preset_id].to_i
When I do this: <%= f.association :user, :collection => User.where(:country_id => 1) %>
My dropdown is populated with lines like this:
#<User:0x0000010b98d170>
Instead, I would like to display an email, that is tied to the id of the users.
I have yet to find how to override the value / content defaults of simple_form when using associations.
Can anyone help?
thank you,
P.
Although the page on github (https://github.com/plataformatec/simple_form) didn't say, but I guess it's the same as the example f.input :age, :collection => 18..60
You could use :label_method and :value_method:
f.association :user, :collection => User.where(:country_id => 1), :label_method => :name, :value_method => :id
I did not use it before. Please tell me if it does not work.