Write checkbox values in database

Hi,

I am new to rails. Any support from your side would be very helpful.

I have a model called child and another model called childcategory. I
have given the code in app/views/children/show.html.erb of child.

<% remote_form_for [@child, Childcategory.new] do |f| %>

<%= f.label :body, " Allowed Categories " %>
<% for category in @cat do %> <%= check_box 'checkbox', category.categoryname %> <%= category.categoryname %> <% end %>

<%= f.submit "Update Settings" %>

<% end %>

NOw the check boxes are displayed from another model called
Filtercategory. When the user checks a check box in the above view, i
want the check box value to be inserted in a model called Childcategory.
The controller code is as follows.

app/controllers/childcategories_controller.rb

class ChildcategoriesController < ApplicationController

def create
@child = Child.find(params[:child_id])
@catlist = @child.childcategories.create!(params[:childcategory])

 respond_to do |format|
   format.html { redirect_to @child }
   format.js
end

end

end

But when i submit the “Update Settings” button, a new record is created
with childid but the category name in the check box is not saved in the
table.

Can you say whats the issue. I couldnt solve this.

Any suggestion would be really supportive.

For starters look at how you are constructing your check_box tags.
The way you have it written now the categories will not be contained
inside the childcategory params. Something like this might get you
headed in the right direction:

<% for category in @categories %>
<%= check_box_tag ‘child_category[category_ids][]’, category.id %> <
%= category.name %>
<% end %>

Then look at generated source for the check boxes and watch the params
that get passed when you submit the form. A single check box is easy,
a series of checkboxes that represent values stored in an association
is much harder and will require some extra work in the view and
possibly the model.

Aaron

Hi Aaron,

      Thanks for your precious time.

I tried your suggestion too. I think i am doing some relationship
mistake between models. I am not sure whether i am doing it in the right
way. I will try to explain what i have done.

Kindly give me whether i am right or whether i am on the wrong way.

I have a models called Child.rb, Childcategory.rb and Filtercategory.rb

The relationships are as given below

class Child < ActiveRecord::Base
belongs_to :user
has_many :childcategories
has_many :timelimits
end

class Childcategory < ActiveRecord::Base
has_and_belongs_to_many :filtercategories
end

class Filtercategory < ActiveRecord::Base

end

NOw i have list of category names in Filtercategories table. I am
listing them using checkboxes in the child view and trying to write them
in Childcategories table if checked by the user.
i.e. app/views/children/show.html.erb

<% remote_form_for [@child, Childcategory.new] do |f| %>

<%= f.label :body, " Allowed Categories " %>
<% for category in @cat %> <%= check_box_tag 'child_category[category_ids][]', category.id %>

<%= category.categoryname %>
<% end %>

<%= f.submit "Update Settings" %>

<% end %>

The controller of Childcategories is as below.

class ChildcategoriesController < ApplicationController

def create
@child = Child.find(params[:child_id])
@catlist = @child.childcategories.create!(params[:childcategory])

 respond_to do |format|
   format.html { redirect_to @child }
   format.js
end

end

end

Hence i have written the code in this manner. But the table is not
updated with categories checked by the user. Only childid is inserted
and new record is created without category name and id.

Kindly help me in this regard.It will be very supportive.

Thanks for any help,

Swarna