niaS
niaS

Reputation: 353

Ruby on Rails: Checkbox gets unchecked when trying to edit a post

When i create a new post (passengerride), it is successfully storing multiple value in database. But, when i try to edit the post (passengerride), the checkboxes are getting unchecked.

This is my form:

apps/views/passengerrides/_form.html.erb:

<%= form_for @passengerride do |f| %>
        ......
          <div class="form-group ">
            <div class="control-label col-sm-2 requiredField">
              <%= f.label :okwithgender, 'Driver should be:' %>
              <span class="asteriskField">*</span>
            </div>

            <div class="col-sm-8">
              <div class=" ">
                <div class="checkbox">

                  <label class="checkbox"><input id="okwithgender"  type="checkbox" value="Male" name="passengerride[okwithgender][]">Male</label>

                </div>
                <div class="checkbox">

                  <label class="checkbox"><input id="okwithgender"  type="checkbox" value="Female" name="passengerride[okwithgender][]">Female</label>

                </div>
                <div class="checkbox">

                  <label class="checkbox"><input id="okwithgender"  type="checkbox" value="Other" name="passengerride[okwithgender][]">Other</label>

                </div>
                <div class="checkbox">

                  <label class="checkbox"><input id="okwithgender"  type="checkbox" value="Any Gender" name="passengerride[okwithgender][]">Any Gender</label>

                </div>
              </div>
            </div>
          </div>

   ....
<% end %>

app/controllers/passengerrides_controllers:

  def passengerride_params
      params[:passengerride][:okwithgender] = params[:passengerride][:okwithgender].join(', ')
      params.require(:passengerride).permit(:origin, :destination ... :okwithgender)
  end

I guess using this <%= f.check_box ... %> will solve the problem, but i'm not sure how to write something equivalent to <input id="okwithgender" type="checkbox" value="Male" name="passengerride[okwithgender][]">

Upvotes: 2

Views: 920

Answers (2)

user372495
user372495

Reputation: 710

I would probably agree that this is easier with Rails form helpers. However, if you don't want to use them, you have to extract the values. So, in your models/passengerride.rb I would add:

def okwithgender_separate
  okwithgender.split(',')
end

and then in your view, each of the inputs would have to have something like:

<input 
  id="okwithgender"  
  type="checkbox" 
  value="Any Gender" 
  name="passengerride[okwithgender][]"
  <%= 'checked="checked"' if @passengerride.okwithgender_separate.include? 'Any Gender' %> >

or you could add that function to a controller helper to clean it up.

Upvotes: 0

Priyank Gupta
Priyank Gupta

Reputation: 421

You should start using rails form helpers for form fields. http://guides.rubyonrails.org/form_helpers.html.

In your case, I think you should try and achieve this using radio buttons as any person will choose just one of these options.

<%= f.radio_button :okwithgender, 'Male' %>
<%= f.label :okwithgender, 'Male', value: 'Male' %>
<%= f.radio_button :okwithgender, 'Female' %>
<%= f.label :okwithgender, 'Female', value: 'Female' %>
<%= f.radio_button :okwithgender, 'Other' %>
<%= f.label :okwithgender, 'Other', value: 'Other' %>
<%= f.radio_button :okwithgender, 'Any Gender' %>
<%= f.label :okwithgender, 'Any Gender', value: 'Any Gender' %>

To achieve this via checkboxes multiple selection,

f.check_box :okwithgender, {multiple: true}, 'Male'
f.label :okwithgender, 'Male', value: 'Male'
f.check_box :okwithgender, {multiple: true}, 'Female'
f.label :okwithgender, 'Female', value: 'Female'
f.check_box :okwithgender, {multiple: true}, 'Other'
f.label :okwithgender, 'Other', value: 'Other'
f.check_box :okwithgender, {multiple: true}, 'Any Gender'
f.label :okwithgender, 'Any Gender', value: 'Any Gender'

Upvotes: 1

Related Questions