Swarna Priya
2009-Feb-26 10:04 UTC
retrieve value from text_field and save it in another model
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sarah Mei
2009-Feb-26 20:06 UTC
Re: retrieve value from text_field and save it in another model
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-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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.[...]> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---