¿Cómo utilizo una casilla de verificación de rieles?

Tengo un controlador:

class PortTestingController < ApplicationController
    def index
        @ports = {"80" => false, "443" => false, "2195" => true, "28009" => false}
    end
end

Esto es lo que tengo en mi opinión:

- @ports.each_with_index do |(key,value), index|
      - fields_for "ports[#{index}]", port do |f|
        f.checkbox "#{key}" "#{key}"
      =key

I've been looking at the documentation for a check_box:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-check_box

and I have no idea what to put write for my checkbox. What's the method? What options can I pass in? What is checked value and unchecked value?

preguntado el 09 de marzo de 12 a las 15:03

1 Respuestas

This is kind of a confusing aspect of ActionView.

Ya que estas llamando f.check_box and not just simply check_box (at least you will be once you fix your typo), the function you are actually calling is this one:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box

Sin embargo, a pesar de la FormBuilder method just passes the object represented by f as the first parameter to a FormHelper. So you can think of it like f.check_box ... es equivalente a check_box f.object ...

El method es la ActionView attribute you want to set.

Hay varios options that can be passed, but I find I generally just pass {}

El checked value is what you want to pass if the checkbox is checked. By default it will be 1. Similarly, unchecked passed by the form if the checkbox is unchecked. Sometimes I find that I want to reverse the polarity of a checkbox and I want to keep track of unchecked boxes, so I set checked a 0 y unchecked a 1.

Now to a more important point. All of these ActionView methods are designed to work with ActiveRecord models which you do not currently have. Your controller isn't setting @ports to a value derived from ActiveRecord::Base. I think you should read up on that some more.

Respondido el 20 de enero de 13 a las 19:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.