Retrieve value from text_field and save it in another model


#1

HI,

  I am very new to rails.

Any help from your side will be very helpful.

I have two models Child.rb and Filtersetting.rb I have written the code
for inserting into both models with many rows at the same time. It works
fine. The issue is that I need the childname entered correspondingly by
the user to be transferred to the Filtersetting table.

How could i do this. Any ideas from your side in this regard will be
very supportive. I have spent around full two days on this.But no
progress.

Please could anyone help me in this regard.

The 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
@children = Array.new(3) { Child.new }
@filters = Array.new(3) { Filtersetting.new }
end

def create
@children = params[:children].values.collect { |child|
Child.new(child) }
@filters = params[:filters].values.collect { |filter|
Filtersetting.new(filter) }
if @filters.all?(&:valid?)

 @filters.each(&:save!)

end
if @children.all?(&:valid?)
@children.each(&:save!)
redirect_to :action => ‘index’
else
render :action => ‘new’
end

end

end

The view is

Apply Children Settings

<%= start_form_tag :action => ‘create’ %>

<% @children.each_with_index do |child, index| %>
<% fields_for "children[#{index}]", child do |f| %> Childname: <%= f.text_field :childname %> <%= f.hidden_field :parent_id, :value => current_user.id %>
   <% end %></td></tr>

<% end %>

<% @filters.each_with_index do |filter, index| %>
<% fields_for "filters[#{index}]", filter do |f| %> Filtering Level: <%= f.collection_select(:levelid, Level.find(:all), :id, :levelname) %>
  <% end %></td></tr>

<% end %>

<%= submit_tag "Submit" %>

<% end_form_tag %>

<%= link_to ‘Back’, children_path %>

The issue is I want to store the childname and childid in Filtersetting
table. That is the first childname and childid corresponding to first
one in Filtersetting table.

I tried out with array and observe field but in vain. Kindly help me in
this regard please.

Regards,
Swarna


#2

I’m not quite sure what you’re after, but my guess is you want to
associate children and filtersetting objects. Have you set up an
association in the model files?

On Thu, Feb 26, 2009 at 2:04 AM, Swarna Priya
removed_email_address@domain.invalid wrote:

I have two models Child.rb and Filtersetting.rb I have written the code
for inserting into both models with many rows at the same time. It works
fine. The issue is that I need the childname entered correspondingly by
the user to be transferred to the Filtersetting table.
[…]