Multiple rows in a model


#1

Hi,

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

I have two models one named Child.rb and the other named
Filtersetting.rb

The general idea is one user can have many children and one child can
have one Filtersetting.

Hence i have done with the User model. Now while adding child to the
user, I need to add many child at a single hit and the Filtersetting for
that child at the same time.

The controller code is as follows,

class ChildrenController < ApplicationController

layout ‘standard’

def index
parent_id = session[:user].id
@children = Child.find(:all, :conditions => [“parent_id = ?”,
parent_id])
if @children.empty?
flash[:notice] = “No child set”
end
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @children }
end
end

def new
@child = Child.new
@level= Level.find(:all)
@cat = Filtercategory.find(:all)
@filter = Filtersetting.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @child }
end
end

def create
@child = Child.new(params[:child])
@child.parent_id = session[:user].id
childname = @child.childname
@filter = Filtersetting.new(params[:filtersetting])
@filter.childname = childname
if @filter.levelid == 1
@filter.levelname = “Elementary School”
end
if @filter.levelid == 2
@filter.levelname = “Middle School”
else
@filter.levelname = “High School”
end

respond_to do |format|
  if @child.save
    @filter.childid = @child.id
    @filter.save
    flash[:notice] = 'Child was successfully created.'
   format.html { redirect_to(@child) }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @child.errors, :status =>

:unprocessable_entity }
end
end
end

end

end

The view code is as follows,

<%= start_form_tag :action => ‘create’ %>
<% (1…3).each do |index| %>

<% fields_for :child do |f| %>

Childname: <%= f.text_field :childname, :index => index %>

<% end %>

<% fields_for :filtersetting do |r| %>

Filtering Level: <%= r.collection_select(:levelid, Level.find(:all), :id, :levelname, {}, :index => index) %>

<% end %> <% end %>

<%= submit_tag "Submit" %>

<% end_form_tag %> <%= link_to 'Back', children_path %>

I get the view displayed fine as i require. But insertion is not done. I
get the error in create function.

I am unable to solve the issue.

Can anyone help me please.

Any suggestion or help from your side will be very supportive.

I have attached the screenshot of the view .

The error is unknown attribute ‘1’

KIndly help me in this regard please.

Regards,
Swarna


#2

in which line do you get the error?

probably here:
@child = Child.new(params[:child])

or here:
@filter = Filtersetting.new(params[:filtersetting])

use the debugger (set it before that line) to see what’s in your
params-hash. there is probably a key like
:1 => “something”
then you can find out, where that attribute is set and correct your
view.