Skip to content

Instantly share code, notes, and snippets.

@dannyvassallo
Last active February 18, 2016 04:35
Show Gist options
  • Save dannyvassallo/e47fab0f2dce9f81ace1 to your computer and use it in GitHub Desktop.
Save dannyvassallo/e47fab0f2dce9f81ace1 to your computer and use it in GitHub Desktop.
How to get Materialize CSS radio buttons in platformatec simple form for Rails 4.
<!-- In your simple_form use this code but change the column name, collection values, and model reference -->
...
<%= f.input :campaign_type, :label => false, :collection => [['Both Media Types', 'Both Media Types'], ['Just Video', 'Just Video'], ['Just Photo', 'Just Photo']], :label_method => :last, :value_method => :first, :as => :materialize_radio_buttons, :checked => @campaign.campaign_type %>
...
class MaterializeRadioButtonsInput < SimpleForm::Inputs::CollectionRadioButtonsInput
# Creates a radio button set for use with Materialize CSS
def input(wrapper_options)
label_method, value_method = detect_collection_methods
iopts = {
:item_wrapper_class => 'field',
:collection_wrapper_class => 'grouped inline fields'
}
return @builder.send(
"collection_radio_buttons",
attribute_name,
collection,
value_method,
label_method,
iopts,
input_html_options,
&collection_block_for_nested_boolean_style
)
end # method
protected
def build_nested_boolean_style_item_tag(collection_builder)
tag = ''
tag << '<p>'.html_safe
tag << collection_builder.radio_button + collection_builder.label
tag << '</p>'.html_safe
return tag.html_safe
end # method
end # class

#Get Dogfalo MaterializeCSS radio buttons in Platformatec simple_form.

Just create an inputs folder in your apps root directory.

Place the file there app/inputs/materialize_radio_buttons_input.rb

Use the :as => :materialize_radio_buttons helper in your simple_form input

@LeoDaoTao
Copy link

Thanks for posting this!!!
I can't get the checked option to work. I tried passing just a string with the radio value and still no joy. Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment