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’ %>
Project Name
<%= text_field :project, :name %>
Project Members
<%= text_field :project, :members %>
Date Started
<%= date_select :project, :date, :order => [:month, :day, :year] %>
Project Description
<%= text_area :project, :description %>
Test Foreign Key
<%= text_field :picture, :comment %>
Test Foreign Key 2
<%= text_field :picture, :comment %>
<%= 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!