Computer Inventory: Problems Retaining Values in a Forms


#1

This is my first rails app and I am creating a Computer Inventory system
as
practice. The problem I am having seems trivial, but I can not figure
out
what exactly is going wrong, so this is where help from those of you who
are
more experienced is needed.

I am having problems retaining the values in some of my forms.
Everything
works fine when I am creating or editing a Computer in the inventory
application. However I am having problems with sub-forms, such as the
ones
to add (new_hardware.rhtml and new_software.rhtml) and modify
HardwareComponent or a SoftwareComponent.

In these sub-forms, which belong to a Computer, I can add
(http://…/new_software/15) a SoftwareComponent without any problems
just as
long as I do not forget any fields. For instance, when adding a
SoftwareComponent, if I forget a field in the form and the user clicks
on
the create button I am presented with the appropriate error messages via
this page (http://…/create_software), but my id from
(http://…/new_software/15) is lost at this point. I know this because
if I
click “create” once more (a second time) I am presented with the error
“Couldn’t find Computer with ID=”. If I enter all the required fields
the
software component gets created properly and linked to its appropriate
computer in the database.

Below I have the code for editing a piece of software that was recently
added to the database. This same behavior I just described above
happens,
but with the additional problem of the values not being returned from
the
database when it is time to edit. If I click the delete button the
software
is properly deleted, so I know I have the right id. I do not know what I
am
doing wrong. Please help.

Below are the models and other pieces in the application worth
displaying.
If anymore information is needed please let me know.

computer.rb

has_many :software_components

software_component.rb

belongs_to :computer

The links in the view to edit a peice of software:
_list_computers.rhtml

…snip…snip…
<%= software[“name”] %> -
<%= software[“license”] %>
(
<%= link_to ‘Edit’,
:action => “edit_software”,
:id => software.id %>

    <%= link_to 'Delete',
     {  :action => "destroy_software",
         :id => software.id
      }, :confirm => "Are you sure?" %>
   ) <br />

…snip…snip…

admin_controller.rb

Any idea why I do not see any values
in the edit form when trying to edit a software that is already in the
database?

def edit_software
@software = SoftwareComponent.find(params[:id])
end

def update_software
@software = SoftwareComponent.find(params[:id])
if @software.update_attributes (params[:software_component])
flash[:notice] = ‘Software was successfully updated.’
redirect_to :action => ‘list_computers’
else
render :action => ‘edit_software’
end
end

Below if the output from my development.log file when trying to debug
this
issue. I followed the same convention for adding and editing a Computer,
which works with no problems.

From File: logs/development.log
===========================

When editing a Computer all the values
appear correctly in the form. When trying
to edit a SoftwareComponent nothing appears
in my form and I get a similiar log message
for the SoftwareComponent as you will see below.

Person Load (0.000681) SELECT concat(last_name, ', ',first_name) as
full_name,
id FROM people ORDER BY last_name ASC

Processing AdminController#edit_computer (for 123.45.234.345 at
2006-03-05
20:31:05) [GET]
Parameters: {“action”=>“edit_computer”, “id”=>“15”,
“controller”=>“admin”}

Computer Load (0.001611) SELECT * FROM computers WHERE (computers.id

‘15’) LIMIT 1
Rendering within layouts/admin
Rendering admin/edit_computer
Computer Columns (0.001455 ) SHOW FIELDS FROM computers
Person Columns (0.001148) SHOW FIELDS FROM people
Rendered admin/_form_new_computer (0.01427)
Completed in 0.02637 (37 reqs/sec) | Rendering: 0.01961 (74%) | DB:
0.00489(18%) | 200 OK [
http://rails.list.help.com/myinventory/admin/edit_computer/15]

Person Load (0.000669) SELECT concat(last_name, ', ',first_name) as
full_name,
id FROM people ORDER BY last_name ASC

Processing AdminController#edit_software (for 123.45.234.345 at
2006-03-05
20:31:15) [GET]
Parameters: {“action”=>“edit_software”, “id”=>“5”,
“controller”=>“admin”}
SoftwareComponent Load (0.000957) SELECT * FROM software_components
WHERE (software_components.id = ‘5’) LIMIT 1
Rendering within layouts/admin
Rendering admin/edit_software
SoftwareComponent Columns ( 0.001039) SHOW FIELDS FROM
software_components
Rendered admin/_form_new_software (0.00235)
Completed in 0.01340 (74 reqs/sec) | Rendering: 0.00883 (65%) | DB:
0.00266(19%) | 200 OK
[http://rails.list.help.com/myinventory/admin/edit_software/5
]

Regards,
Arwuah _


#2

On Mar 6, 2006, at 1:50 PM, Arwuah _ wrote:

SoftwareComponent.
Computer with ID=". If I enter all the required fields the software
component gets created properly and linked to its appropriate
computer in the database.

Your create_software action should be redirecting back to
new_software if the save fails. Something like:

if software.save
blah blah…
else
redirect_to :action => “new_software”
end

In other words, you shouldn’t be click a create button when the url
shows http:// …/create_software

You don’t show your code for the edit_software view, but I’m guessing
that you didn’t supply the :id parameter to the form_tag helper.
Something like:

<%= start_form_tag :action => “update_software”, :id => @software %>

-Derrick S.


#3

On 3/6/06, Derrick S. removed_email_address@domain.invalid wrote:

Everything works fine when I am creating or editing a Computer in
appropriate error messages via this page (http://…/
create_software), but my id from (http://…/new_software/15) is
lost at this point. I know this because if I click “create” once
more (a second time) I am presented with the error “Couldn’t find
Computer with ID=”. If I enter all the required fields the software
component gets created properly and linked to its appropriate
computer in the database.

Your create_software action should be redirecting back to
new_software if the save fails. Something like:

Derrick, thanks for the quick response. My create_software action is
just as
you suggested:
…snip…snp…
def create_software
@software = SoftwareComponent.new(params[:software_component])
@computer = Computer.find(params[:computer_id])
@computer.software_components << @software

if @computer.save
  flash[:notice] = "Software was successfully created."
  redirect_to :action => 'list_computers'
else
  render :action => 'new_software'
end

end
…snip…snp…

if software.save

Something like:
And my “edit_software view” is just as you pointed out:

<%= start_form_tag :action => ‘update_software’, :id => @software %>
<%= render :partial => ‘form_new_software’ %>
<%= submit_tag ‘Commit Change(s)’ %>
<%= end_form_tag %>

<%= link_to ‘Show’, :action => ‘show_software’, :id => @software %> |
<%= link_to ‘Back’, :action => ‘list_computers’ %>

<%= start_form_tag :action => “update_software”, :id => @software %>

Additionally here is my new_software.rhtml

New Software

<%= start_form_tag :action => ‘create_software’ %>
<%= render :partial => ‘form_new_software’ %>
<%= submit_tag “Create” %>
<%= end_form_tag %>

<%= link_to ‘Back’, :action => ‘list_computers’ %>

I am still having the problems pointed out in my original email. Any
ideas
on what else I could be overlooking?

Thanks,
-Arwuah _

-Derrick S.