Hello, I was talking with someone in IRC, trying to get one form with however many fields to make new rows. He worked with me on the controller, but then I didn''t get the help with the view I needed, and I''ve been trying to do it on my own. I''m uploading pictures to my Pictures table. As you know, I need a few fields for each picture. I''ll need the filename, content-type, comment, id, and foreign key referencing the project it is in. This is my controller. class ProjectsController < ApplicationController before_filter :login_required, :except => [ :list, :index ] def index end def new @project = Project.new @picture = Picture.new end def create @project = Project.new(params[:project]) if @project.save then (params[:pictures] || []).each { |pictureParam| @project.pictures.create(pictureParam) } @project.pictures.create(params[:picture]) if params[:picture] end render :action => ''new'' end def list @project_pages, @projects = paginate(:projects, :order => "date ASC", :per_page => 5) end end In ''new.rhtml'' I have this. <%= start_form_tag :action => ''create'' %> <p>Project Name</p> <%= text_field :project, :name %><br /> <p>Project Members</p> <%= text_field :project, :members %><br /> <p>Date Started</p> <%= date_select :project, :date, :order => [:month, :day, :year] %> <p>Project Description</p> <%= text_area :project, :description %><br /> <p>Test Foreign Key</p> <%= text_field :picture, :comment %><br /> <p>Test Foreign Key 2</p> <%= text_field :picture, :comment %><br /> <%= submit_tag "Add" %> <%= end_form_tag %> When I submit that, the foreign key table populates correctly, but only once. Since they''re being submitted the same way, they overwrite eachother and I''m left with one new row in my Pictures table. Any idea what he was going for with that controller? What type of thing should I do so I can have 4-5 fields each make a new row. He was saying something about an array in the view. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/a0489721/attachment.html
Hi Matt, I think what you''re looking for may be... <%= text_field :project[], :name %><br /> According to AWDwRoR, p. 356, the brackets tell "Rails to include the object''s id as part of the field name. ... When the form is submitted to the controller, params[:product] will be a hash of hashes, where each key is the id of a model object and the corresponding value are the values from the form for that object. In the controller, this could be used to update all product rows with something like Product.update(params[:product].keys, params[:product].values HTH, Bill ----- Original Message ----- From: Matt Ramos To: Rails@lists.rubyonrails.org Sent: 2006-03-18 8:49 AM Subject: [Rails] Need help with my view populating rows Hello, I was talking with someone in IRC, trying to get one form with however many fields to make new rows. He worked with me on the controller, but then I didn''t get the help with the view I needed, and I''ve been trying to do it on my own. I''m uploading pictures to my Pictures table. As you know, I need a few fields for each picture. I''ll need the filename, content-type, comment, id, and foreign key referencing the project it is in. This is my controller. class ProjectsController < ApplicationController before_filter :login_required, :except => [ :list, :index ] def index end def new @project = Project.new @picture = Picture.new end def create @project = Project.new(params[:project]) if @project.save then (params[:pictures] || []).each { |pictureParam| @project.pictures.create(pictureParam) } @project.pictures.create(params[:picture]) if params[:picture] end render :action => ''new'' end def list @project_pages, @projects = paginate(:projects, :order => "date ASC", :per_page => 5) end end In ''new.rhtml'' I have this. <%= start_form_tag :action => ''create'' %> <p>Project Name</p> <%= text_field :project, :name %><br /> <p>Project Members</p> <%= text_field :project, :members %><br /> <p>Date Started</p> <%= date_select :project, :date, :order => [:month, :day, :year] %> <p>Project Description</p> <%= text_area :project, :description %><br /> <p>Test Foreign Key</p> <%= text_field :picture, :comment %><br /> <p>Test Foreign Key 2</p> <%= text_field :picture, :comment %><br /> <%= submit_tag "Add" %> <%= end_form_tag %> When I submit that, the foreign key table populates correctly, but only once. Since they''re being submitted the same way, they overwrite eachother and I''m left with one new row in my Pictures table. Any idea what he was going for with that controller? What type of thing should I do so I can have 4-5 fields each make a new row. He was saying something about an array in the view. Thanks! ------------------------------------------------------------------------------ _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060318/e526fa86/attachment.html
When I inserted that text field I got an error saying [] is undefined. Also.. How much of my controller would I have to change? I''m still really new to rails, so could you describe a little more? Thanks, Matt On 3/18/06, Bill Walton <bill.walton@charter.net> wrote:> > Hi Matt, > > I think what you''re looking for may be... > > <%= text_field :project[], :name %><br /> > > According to AWDwRoR, p. 356, the brackets tell "Rails to include the > object''s id as part of the field name. ... When the form is submitted to > the controller, params[:product] will be a hash of hashes, where each key is > the id of a model object and the corresponding value are the values from the > form for that object. In the controller, this could be used to update all > product rows with something like > Product.update(params[:product].keys, params[:product].values > > HTH, > Bill > > ----- Original Message ----- > *From:* Matt Ramos <matt.ramos@gmail.com> > *To:* Rails@lists.rubyonrails.org > *Sent:* 2006-03-18 8:49 AM > *Subject:* [Rails] Need help with my view populating rows > > Hello, I was talking with someone in IRC, trying to get one form with > however many fields to make new rows. He worked with me on the controller, > but then I didn''t get the help with the view I needed, and I''ve been trying > to do it on my own. > > I''m uploading pictures to my Pictures table. As you know, I need a few > fields for each picture. I''ll need the filename, content-type, comment, id, > and foreign key referencing the project it is in. > > This is my controller. > > > > class ProjectsController < ApplicationController > before_filter :login_required, :except => [ :list, :index ] > > def index > end > > def new > @project = Project.new > @picture = Picture.new > end > > def create > @project = Project.new(params[:project]) > if @project.save then > (params[:pictures] || []).each { |pictureParam| > @project.pictures.create(pictureParam) > } > @project.pictures.create(params[:picture]) if params[:picture] > end > render :action => ''new'' > end > > def list > @project_pages, @projects = paginate(:projects, :order => "date ASC", > :per_page => 5) > end > > end > > > > In ''new.rhtml'' I have this. > > <%= start_form_tag :action => ''create'' %> > <p>Project Name</p> > <%= text_field :project, :name %><br /> > <p>Project Members</p> > <%= text_field :project, :members %><br /> > <p>Date Started</p> > <%= date_select :project, :date, :order => [:month, :day, :year] %> > <p>Project Description</p> > <%= text_area :project, :description %><br /> > <p>Test Foreign Key</p> > <%= text_field :picture, :comment %><br /> > <p>Test Foreign Key 2</p> > <%= text_field :picture, :comment %><br /> > <%= submit_tag "Add" %> > <%= end_form_tag %> > > When I submit that, the foreign key table populates correctly, but only > once. Since they''re being submitted the same way, they overwrite eachother > and I''m left with one new row in my Pictures table. > > Any idea what he was going for with that controller? What type of thing > should I do so I can have 4-5 fields each make a new row. He was saying > something about an array in the view. > > Thanks! > > ------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- -Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060321/01137bde/attachment-0001.html