Marco
Marco

Reputation: 10813

CheckBox_Tag in Rails

I'm using rails 3.1 and ruby 1.9.2. Using checkbox_tag I want that when I click on checkbox, a variable (says @post) is set true then it's passed to controller. How can I do this?

Upvotes: 0

Views: 801

Answers (2)

numbersnelson
numbersnelson

Reputation: 161

Here is an example of jquery and view code of a live edit/save from a view using ajax:

In application.js:

 jQuery('#tranx_field').live('change', function() {
    var attr_value = $(this).val();
    var attr_name =  $(this).next("input[id=attr_name]").val();
    var tranx_id = $(this).parent().parent().parent().children("input[id=tranx_id]").val();

    $.ajax({
        url: "/tranxes/" + tranx_id,
        dataType: "json",
        type: "PUT",
        processData: false,
        contentType: "application/json",
        data: "{\"tranx\":{\"" + attr_name + "\":\"" + attr_value + "\"}}"
    });
 });

And the view code:

<%= hidden_field_tag 'tranx_id', @tranx.id %>
    <div class="row">
      <div class="span6 id="left_col">
            <b>Salesrep:</b>
                <%= text_field_tag 'tranx_field', @tranx.salesrep,:class => "itemfieldsm" %>
                <%= hidden_field_tag 'attr_name', "salesrep" %>
            <br>

Upvotes: 2

wael34218
wael34218

Reputation: 4930

In the view you could add the following

hidden_field_tag 'post', false
check_box_tag 'post', true

in the controller when the form is submitted, you could just assign it:

@post = params[:post]

The hidden value is just to set the default value, which will be overwritten if the checkbox is selected

Upvotes: 0

Related Questions