Jhonny Pamponet
Jhonny Pamponet

Reputation: 83

how to check check_box_tag is checked or not in rails

how to check check_box_tag is checked or not in rails?

I need to know what checkboxes is checked.

This system is to users generate reports baseds on options selects.

View:

<%= form_tag finish_reports_path do %>
    <div class="row top30 reload">
        <div class="col-sm-12 col-xs-12">
            <div class="form-inline">
                <%= check_box_tag("name", "1", false, class: "checkbox") %>
                <%= label_tag :name, "Nome do Conteúdo" %>
            </div>
            <div class="form-inline">
                <%= check_box_tag(:url, "2", false, class: "checkbox") %>
                <%= label_tag :url, "URL do Conteúdo" %>
            </div>
            <%= link_to t("button.generate"), finish_reports_path(finish: "yes"), :class => "btn btn-success" %>
        </div>
    </div>
<% end %>

Controller:

   def index
    end

    def finish
        redirect_to root_path
    end

Routes:

root "reports#index"
post "/" => "reports#read_combo", as: :read_reports_combo

get "/reports/generate/:type" => "generate#index", as: :reports_generate
get "/reports/generate" => "generate#finish", as: :finish_reports

Upvotes: 1

Views: 4324

Answers (2)

Jhonny Pamponet
Jhonny Pamponet

Reputation: 83

I use this code in View:

                <div class="form-inline">
                    <%= check_box_tag("name[]", "i18n.name", false, class: "checkbox") %>
                    <%= label_tag :name, "Nome do Conteúdo" %>
                </div>
                <div class="form-inline">
                    <%= check_box_tag("name[]", "url.url", false, class: "checkbox") %>
                    <%= label_tag :url, "URL do Conteúdo" %>
                </div>

And this in Controller:

@properties = params["name"]

And solved =D!!

Upvotes: 0

fishermanswharff
fishermanswharff

Reputation: 125

If the checkbox isn't checked, no params are passed, meaning the value is false. If the checkbox gets checked, the params will show up in the controller. If you want to pass the falsy value to the params, create a hidden field with the hidden_input value set to false. I believe this is a duplicate. Rails checkbox and params

Upvotes: 1

Related Questions