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’ %>

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!

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

Hi Matt,

I think what you’re looking for may be…

<%= text_field :project[], :name %>

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 R.
To: [email protected]
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’ %>

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!



Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails