Inserting parent_id


#1

I have two tables:

  • projects
  • lines

lines has a foreign key constraint to projects.id

When I’m creating a new line object, the database fails on insert with
the following error:

“PGError: ERROR: null value in column “project_id” violates not-null
constraint”

My code looks like this:

def create_lines
@project = params[:id]
@line = Line.new(params[:line])
if @line.save
flash[:notice] = ‘Lines was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

Here’s the template:

<%= form_tag :action => ‘create_lines’, :id => @project %>

<% while @count < @line_qty %>
<tr>
    <td><%= text_field("line", "partno", "size" => 20) %></td>
    <td><%= text_field("line", "qty", "size" => 20) %></td>
    <td><%= text_field("line", "target_price", "size" => 20) %></td>
    <td><%= text_field("line", "condition", "size" => 20) %></td>
</tr>

<% @count += 1 %>
<% end %>



<%= submit_tag(" ADD LINES ") %>
<%= end_form_tag %>

Any idea why it doesn’t pickup the parent id ?

/mich


#2

mich wrote:

  redirect_to :action => 'list'

    <td colspan="4"><%= submit_tag(" ADD LINES ") %></td>
</tr>
<%= end_form_tag %>

Any idea why it doesn’t pickup the parent id ?

/mich

Either I’m missing something here, or you really don’t assign a project
to
the line. The params[:line] you’re using to create a new Line doesn’t
include a project.

I don’t know how you’re interface is set up, but either include a field
for
the project (usually a select), or add “@line.project = @project
between
the save and new statements of the line.

There is no need for the @project and @lines variables in the
create_lines
method to be isntances variables BTW. You can remove the @'s.


#3

Wiebe C. wrote:

Either I’m missing something here, or you really don’t assign a project
to
the line. The params[:line] you’re using to create a new Line doesn’t
include a project.

I don’t know how you’re interface is set up, but either include a field
for
the project (usually a select), or add “@line.project = @project
between
the save and new statements of the line.

Beautiful - that did the trick. Pretty obvious, it seems now :wink:

There is no need for the @project and @lines variables in the
create_lines
method to be isntances variables BTW. You can remove the @'s.

That makes sense, thanks.

However, I still seem to have issues inserting the rows. As you can see
from my code snippets, I’d like to insert several line items…

<% while @count < @line_qty %>

<tr>
    <td><%= text_field("line", "partno", "size" => 20) %></td>
    <td><%= text_field("line", "qty", "size" => 20) %></td>
    <td><%= text_field("line", "target_price", "size" => 20) %></td>
    <td><%= text_field("line", "condition", "size" => 20) %></td>
</tr>

<% @count += 1 %>
<% end %>

How do I tell the create_lines action to process all the lines, and also
how do I send multiple lines, with multiple items to the controller ?

Thanks in advance,

/mich


#4

On Thursday 06 April 2006 22:07, mich wrote:

</tr>

Hmm, that’s going to be a bit difficult to explain. I have a similair
need in
an app I’m making. What I did was use hashes.

In your view, you will have to make a hash, “lines” for example, with
“line.id.to_s” as key. You can then supply it to a text_field_tag like
so:

text_field_tag(“lines[#{line.id}]”, rest_of_options)

The object “line” is a parameter of a block (is that called a parameter?
The
thing between |'s?) for example, when iterating over a collection of
lines.
This way, you can store a collection of fields in a hash, which you can
unpack
again in your controller.

I know this may be of little help, but as I said, it’s going to be a
little
difficult to explain. Also, I’d need more info on your app to give
accurate
help, but that would most likely mean a lot of work for me. I do need
time to
work on my own app, you see :slight_smile: