Is there a way to group inputs under one label and display in inline? To avoid something like this:
.input.optional
= form.label :price
.input
= form.text_field :price
= form.select :currency
There's nothing in Formtastic for this, but you can achieve the same visual affect by using two standard Formtastic inputs one after the other and styling them to appear "inline". This means you might want to wrap them in a containing field set, float them against each other, and visually hide one or both of the labels.
Related
In rails both label and label_tag seem to work the same. Are there any internal differences on how they are rendered by rails ? And which one is a better to use ?
Use f.label when you are inside a form object created with form_for(...) do |f| and want to refer to a model-attribute. If your app is i18n-ed, Rails will use the translation to display the attribute name.
Use label_tag when you are not in a form object. (Or you are in a form object but want to create a dummy label for a non-model attribute.)
All form inputs have these two variants, with and without the _tag suffix, like select and select_tag, etc.
I'm assuming you mean just label and not f.label.
The difference I have seen between using only label and label_tag is that you cannot set custom labels while using only label i.e if you use
label :name, "My Name:
in the view, it will not render My Name but just Name.
But if you use
label_tag :name, "My Name:"
It will render My Name on the display.
I have a Rails app that pulls in music from Soundcloud. This data contains a title, which I save as mix.sc_title but it's not always properly formatted. I have added an additional attribute on my Mix model which I call mix.override_title
For display on my site, I want to use the override title if available, and the sc_title in all other cases.
I have a Mix model method to do this for me
def display_title
override_title.blank? sc_title : override_title
end
Mixes#index grabs #mixes = Mix.where(:active => true) and mixes/index.html.erb looks like this:
<ul>
<% #mixes.each do |mix| %>
<li><%= link_to mix.display_title, mix %></li>
<% end %>
</ul>
As you can see, I'm not directly using any mix attributes, and so I take a huge hit when I go to the DB, and I don't actually benefit from it.
Is there a leaner way to get just the information I need? (mix.display_title)
I have tried Mix.select("display_title").where(:active => true) but it fails because display_title is not a real DB column
You can do Mix.select("sc_title, override_title").where(:active => true) and it will work, since those are the actual fields that the method uses. I don't really think getting the additional attributes gives you that much of a DB hit but sometimes selecting only what you need can be beneficial.
As you start chaining on more Arel commands, consider putting the select into a model method:
def select_active_titles
select("sc_title, override_title").where(:active => true)
end
Edit: Your link_to helper also secretly calls mix.id to link to the right mix, so make sure it's working and if not add id to the list of selected attributes.
I'm using the simple_form gem.
I'm rendering an input based on a collection (a list of all my actiontypes)
<%= f.association :actiontype, collection: Actiontype.all, input_html: { data: {'impacts-pnl' => ??}} %>
I would like to be able to add a data-attribute to the input to store extra data.
In this case, I want to store the impacts_pnlattribute of my actiontype. The only problem is that I don't know how to refer to the current actiontype
collection.impacts_pnl doesn't work (obviously)
actiontype.impacts_pnlneither.
how can I pass this extra bit of data to my input?
If you want to add these attributes to the option-elements of a selectfield, you should alter the collection by using for example the .map() function. Also, use the input helper with block to do this, otherwise it doesn't work;
= f.input :actiontype do
= f.select :actiontype, Actiontype.all.map{|a| [a.name, a.id, {"data-impacts-pnl" => p.impacts_pnl}]}
For more information about this issue, see https://github.com/plataformatec/simple_form/issues/188
I have a GroupCoach model, Group Coaches has_many :groups. On my new Group form I want to pass a group_coach_id to the Group object in a hidden field so that a group gets associated with a GroupCoach without the user having to select one.
So in my Groups_Controller
#group = Group.new
#group_coach = GroupCoach.first(:order => "RAND()")
This will get a random GroupCoach. and then in the new Group view I have a hidden field
<%= f.hidden_field #group_coach %>
This obviously doesn't work 100% right. It does pass the group_coach_id but its not telling the form what column to save it in...
I have also heard this is very insecure...
Make a token column. Simply SHA1 encrypt it (or whatever your choice is) and pass that instead. It's much harder to guess.
I used the following code to resolve this issue
<%= f.hidden_field :group_coach_id, :value => #group_coach.id %>
But is this the most secure? Seems pretty insecure as I could change the value in Firebug or something...
What I use rails form helpers to build a form, input elements in that form have id attributes so they can match with their labels. The problem is this id is the something like person_first_name and not person_first_name_#{person.id} meaning if I have more than one form of the same type on the same page unexpected things can happen.
A perfect example of this is using jquery-ui datepicker. I have a series of forms all containing a text_field element wrapped in a div with the class datepicker. I apply the datepicker like this (in document ready) $('.datepicker input').datepicker(options) and guess what, every one of these elements, although has a seemingly working datepicker (click on input, datepicker appears), although when a date is selected in any of these datepickers only the first element on the page (of that element type, ex. input id=published_on) gets updated with the value.
Any suggestions on getting rails to output more unique element id's or make datepicker not use the id attribute?
You can easily customize the id of your input.
text_field(:person, :first_name, :id => "person_first_name_#{person.id}")
One would think that if you set the ID of the form to say :id => "edit_person_#{person.id}" that this ID would cascade downwards into the label and field logic. From what I can see, there's too much auto-magic in the way of adding an eloquent custom solution.
# Haml
= form_for #model do |f|
.field
= f.label :field_name
= f.text_field :field_name
When using the above example, ids are rendered as expected.