简体   繁体   中英

Rails: simple_form radio buttons

I want to use my custom button as radio buttons in a simple form in rails. But I really don't know how to do this. I have a CSS code that gives me the button I want, but I think my simple form is wrong and I don't really know how to set up the javascrip...

Does someone know how I can get this right?

My simple form:

  <%= simple_form_for(@post, :html => {:class => 'form-inline' }) do |f| %>
    <%= f.input :link, label: false %>
    <div class="button_group">
      <%= f.input :type, as: :hidden %>
      <a class="radio_button" data-value="0">ONE</a>
      <a class="radio_button" data-value="1">TWO</a>
      <a class="radio_button" data-value="2">THREE</a>
    </div>
    <%= button_tag(type: 'submit', class: "submit_button") do %>
      SUBMIT!
    <% end %>
  <% end %>

CSS button:

.radio_button {
    color: white;
    background-color:#828282;
    font-family: 'Fjalla One', serif;
    font-size: 10pt;
    font-weight: 100;
    text-align: center;
    font-style: italic;
    padding:0px 6px;
    padding-right: 8px;
    padding-top: 3px;
    padding-bottom: 2px;
    text-decoration:none;
    border: none;
}.radio_button:hover {
    background-color:#0E9EBB;
    color: white;
    text-decoration:none;
}.radio_button:active {
    position:relative;
    background-color:#0E9EBB;
    top:1px;
}

See this commented code:

// select all next .radio_button elements to #post_type hidden input
$('#post_type ~ a.radio_button').

  // execute code block on click
  click( function(){

     // this refers to clicked a element, you will get strings like: 0, 1, 2
     var clickedValue = $(this).data('value');

     // set clicked value in hidden input
     $('#post_type').val(clickedValue);

  } );

Update

because when you have simple_form_for @something and @something if from class Something , f.input :some_field input is generated with id: something_some_field , you can check you generated HTML to confirm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM