Forum: Ruby on Rails retrieve value from text_field and save it in another 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-26 11:04
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

<h1>Apply Children Settings</h1>


<%= start_form_tag :action => 'create' %>
<table align="left">
<% @children.each_with_index do |child, index| %>
     <tr> <td><% 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 %>
 </table>
<table align="right">
<% @filters.each_with_index do |filter, index| %>
     <tr><td> <% fields_for "filters[#{index}]", filter do |f| %>
         Filtering Level: <%= f.collection_select(:levelid,
Level.find(:all), :id, :levelname) %>


      <% end %></td></tr>
<% end %>
</table>
<p>
    <%= submit_tag "Submit" %>
  </p>
<% 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
92497f51734ed56687928d5fd68d870a?d=identicon&s=25 Sarah Mei (Guest)
on 2009-02-26 21:07
(Received via mailing list)
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
<rails-mailing-list@andreas-s.net> 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.
[...]
This topic is locked and can not be replied to.