I tried to simplify my code from this:
button_tag default_button[:name] ? default_button[:name] : t(button.to_s) , type: default_button[:type] ? default_button[:type] : 'submit', class: default_button[:class] ? default_button[:class] : "btn_#{button.to_s}"
To this:
button_tag(default_button)
But it doesn't work, I get the whole hash as text in the button. How can I make it smart to use the hash key/values?
I found this solution, the first parameter is a bit special.
concat button_tag(default_button[:name], default_button)
Related
I currently have a select in a form which works fine:
<%= f.select(:scan_type, options_for_select(Scan::SCAN_TYPES, task.scan_type)) %>
I want to convert it to a set of radio buttons, as there are only a few options. Is there a way to use options_for_select with collection_radio_buttons?
I'm just using a simple array for my options, i.e. in scan.rb -
SCAN_TYPES = ['roll', 'single']
My first approach was to try
<%= f.collection_radio_buttons(:scan_type, options_for_select(Scan::SCAN_TYPES, object.scan_type)) %>
But I'm not providing all the arguments. I'm at a loss to see what needs to be added.
f.collection_radio_buttons(:scan_type, Scan::SCAN_TYPES.map{|s| [s, s] }, checked: f.object.scan_type)
Check this
I have an image_tag in rails. I have a jquery plugin that requires me to add a tag data-elem=pinchzoomer to enable touch zoom. How do I add this to the below?
image_tag(#listing.image.url, class: "rsTmb img-responsive")
Like so:
image_tag(#listing.image.url, class: "rsTmb img-responsive", data: {elem: 'pinchzoomer')
For data elements, just add the key 'data' to the hash you're passing to the helper method, and use the bit after "data-" to be the keys to the hash. So, if you wanted to have data-foo='fred' data-bar='barney', you'd add:
data: {foo: 'fred', bar: 'barney'}
Given a hash of properties for a text_field to be made in HAML:
entry = {class: "form-control", disabled: true, id: :foobar}
How can I do this:
%dd!= f.send(:text_field, entry[:id], class: entry[:class], disabled: entry[:disabled])
But flexible? (So don't need placeholders, just a hash). Have looked at simple form docs and action view form helper docs and found input_html, but that's not working in this manner.
The f.send is obligatory, as :text_field can be anything. Would prefer not to use eval
text_field takes:
text_field(attribute_name, input_html_options)
Does
f.text_field(entry[:id], entry)
not work?
I'm working on a legacy project that is using acts_as_taggable_on which expects tags to come in arrays. I have a select box allowing users to select a tag on a Course in a field called categories. The only way mass assignment create will work is if params looks like this params = {:course => {:categories => ['Presentation']}}. I've currently a view with this helper:
<%= f.select 'categories', ['Presentation' , 'Round Table' , 'Demo', 'Hands-on'] %>
Which will give me a parameter like params = {:course => {:categories => 'Presentation'}}. This doesn't work since Acts as tag gable apparently can't handle being passed anything other than a collection.
I've tried changing categories to categories[] but then I get this error:
undefined method `categories[]' for #<Course:0x007f9d95c5b810>
Does anyone know the correct way to format my select tag to return an array to the controller? I'm using Rails 3.2.3
I didn't work with acts_as_taggable_on, but maybe this simple hack will be suitable for you? You should put it before mass-assignment.
category = params[:course][:categories]
params[:course][:categories] = [category]
If you are only going to allow the selection of ONE tag, you could do:
<%= f.select 'categories', [['Presentation'] , ['Round Table'] , ['Demo'], ['Hands-on']] %>
Each one item array will have first for the display value, and last for the return value, which in this case will both return the same thing, as the first element of the array is the same as the last element when the array as one element.
Seems like select doesn't give you that option.
If I understand correctly, one option might be to use a select_tag instead and just be explicit about where you want the selection in the params:
<%= select_tag 'course[categories][]', options_for_select(['Presentation' , 'Round Table' , 'Demo', 'Hands-on']) %>
That ought to get your params the way you need them.
Here's what I'm using for one of my projects:
<% options = { include_blank: true } %>
<% html_options = { required: true, name: "#{f.object_name}[#{resource.id}][days][]" } %>
<%= f.select :days, DAYS, options, html_options %>
Without html_options[:name], Rails handles the name of the select tag and spits out something like
service[service_add_ons_attributes][11][days]
but I need
service[service_add_ons_attributes][11][days][]
So I override it.
Hope that helps.
Here is my div. What is the syntax for adding another conditional class?
.progress{ :class => list.overdue? ? "progress-danger" : "" }
I essential want to add list.dirty? ? "progress-warning" : ""
But what is the right way to fit that in along side the list.overdue? part?
Why dont you take it to a helper
View code
.progress{ :class => check_list_over_due }
Helper Code
def check_list_over_due
if condition
'classname'
elsif condition
'classname'
else
'classname'
end
end
I don't know if it's "right" per se, but this should work:
:class => [list.overdue? ? "progress-danger" : nil,
list.dirty? ? "progress-warning" : nil].compact.join(" ")
I think the best way is to write helper, like "progress_bar_class(list)" and implement all logic in light structurized code.