Forum: Ruby on Rails Need help with my view populating rows

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.
Matt R. (Guest)
on 2006-03-18 16:51
(Received via mailing list)
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!
Bill W. (Guest)
on 2006-03-18 17:25
(Received via mailing list)
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 R.
  To: removed_email_address@domain.invalid
  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
  removed_email_address@domain.invalid
  http://lists.rubyonrails.org/mailman/listinfo/rails
Matt R. (Guest)
on 2006-03-21 02:15
(Received via mailing list)
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
This topic is locked and can not be replied to.