Forum: Ruby on Rails Multiple rows in a model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C1d4927c1321010d0b15d4981eede378?d=identicon&s=25 Swarna Priya (rmswarna)
on 2009-02-25 11:54
Attachment: Screenshot-2.png (90 KB)
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| %>
<tr>
  <% fields_for :child do |f| %>
  <td><p>
    Childname:
    <%= f.text_field :childname, :index => index %>
  </p></td>
  <% end %>

<% fields_for :filtersetting do |r| %>
  <td><p>
    Filtering Level:
    <%= r.collection_select(:levelid, Level.find(:all), :id, :levelname,
{}, :index => index) %>
  </p></td>
<% end %>
<% end %>
</tr>
 <tr><td> <p>
    <%= submit_tag "Submit" %>
  </p></td></tr>
<% end_form_tag %>
</table>
<%= 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
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2009-02-25 15:15
(Received via mailing list)
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.
This topic is locked and can not be replied to.