marie
marie

Reputation: 231

Check box - not changing boolean value

I have a form where a user has to check condition of sales before submit

 <%= form_for @order, url: checkout_path do |f| %>                      
  <%= f.hidden_field :user_id %>
  <%= f.check_box :cgos_accepted %>
  <%= f.submit %>
<% end %>

:cgos_acceptedis an attribute of order which is a boolean and by default it's false.

When the user check the box it has to change the value to true, but it doesn't...

EDIT >

this is the html when checkbox is checked:

<input class="form-check-input boolean optional" type="checkbox" value="true" name="order[cgos_accepted]" id="order_cgos_accepted" data-com.agilebits.onepassword.user-edited="yes">

I add my controller maybe something is wrong and I don't see...

class Clients::OrdersController < Clients::ApplicationController

    def index
        @orders = Order.all
        @orders = @orders.filter_by_status(params[:status]) if params[:status]
    end

    def show
        @order = Order.find(params[:id])
    end

    def new
        @order = current_cart.order
        @billing_address = BillingAddress.new
    end

    def create
        @order = current_cart.order
        @order.update_sub_total!
        @order.update_total!
        if @order.update_attributes!(user_id: current_user.id)
        redirect_to new_clients_order_payment_path(@order)
        end
    end

    private

    def order_params
        params.require(:order).permit(:status,  :user_id, :token , :sub_total, :cgos_accepted)
    end
end

Upvotes: 1

Views: 1350

Answers (2)

johan
johan

Reputation: 721

Good you added your controller

In the create action you should change:

 def create
    if @order.update_attributes!(order_params.merge(user_id: current_user.id))
     redirect_to new_clients_order_payment_path(@order)
    end
 end

You weren't passing the strong parameters, so it couldn't update

and as Ray suggested, use:

= f.check_box :cgos_accepted, { class: "class-name", style: "style"}, "checked-value", "unchecked-value"

Upvotes: 0

ray
ray

Reputation: 5552

use f.check_box as below,

= f.check_box :cgos_accepted, { class: "class-name", style: "style"}, "checked-value", "unchecked-value"

Upvotes: 1

Related Questions