radiobuttonlist - Example of a rails custom formbuilder for a radiobutton list -


I'm looking for an example how to create a custom form builder for a radio button list bound to an action For actions_as_tree I already have a code that displays the radio button list, but want to dry it by refactoring in Forbizbiller

Any signal / link will be appreciated.

Assuming that you are calling form_by many different views for this model and each There is only one set of radio buttons in the form. Here are some routes you can take here. However, I can not tell whether you are asking how to automatically add the radio button list or just summarize it.

To add the radio button list automatically, I need a little more knowledge of ActionView internals. Basically you have to find out how things_for methods in the ActionView :: Helpers :: FormHelper are provided and overrides to work properly. Unfortunately if FormBuilders do not work this way, then it is much more difficult than the price Will be.

One better way is to create a method that generates all your radio buttons in your custom form builder, to keep things simple in the event of change, we want to continuously add to the model. I have arbitrarily decided that it will be a hash where each key is an attribute method that you want to represent in the form with a radio button list, and each value is a list of acceptable values ​​for that attribute. This form makes it easy to generate many lists of radio buttons without changing the builder. You can also use hash to validate the content of any feature associated with radio buttons.

/app/models/example.rb

  class example & lt; ActiveRecord :: Base ... RADIO_BUTTON_HASH = {: attribute_a = & gt; [: Acceptable_value_a1 ,: approved_value_a2 ,: approved_value_a3] ,: attribute_b = & gt; [: Acceptable_value_b1 ,: accepted_value_b2], ...} def self.radio_buttons_sort (a, b) # Sort method to ensure that many radio button lists appear in the order that you defself.radio_buttons_sorted RADIO_BUTTON_HASH. Sort {| A, b Radio_buttons_support (a, b)} end ... end  

I added some feature methods in addition to hash, they are not required but they provide a cleaner method of standardizing the output .

** / app / helpers / example_helper.rb module ExampleHelper

  # custom formbuilder class ExampleFormBuilder & lt; ActionView :: Helpers :: Formbilder # End-Coppers and Maximum Length of Set Field 3 All Text Area Referred_button_list (option = {}) Example.sorted_radio_buttons.each do | Method, value. Values Value | Price |  

Then from an example where ExampleHelper is included (the example is by default in any view provided by the controller).


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -