Lance Squire
2006-Apr-07 18:01 UTC
[Rails] Formatting form tags for children of parrent data?
This my first Rails project.
All my previous work was done it Perl, so I may be missing the
Ruby/Rails obvious here.
The site I''m working on needs to display archived programs.
Some of the programs have multiple versions, all belonging to the same
listing.
As such, I''ve set-up an extra_files table related to the archive list.
Displaying the extra files was trivial. However, when I went to edit the
edit.rhtml file, I had no idea how to format the text input fields so
that they would be automaticly picked-up and placed with the parrent.
Here''s what I have:
archives table:
id program_name file_location etc...
archives model:
class Archive < ActiveRecord::Base
belongs_to :category
has_many :extra_file
end
extra_files table:
id archive_id name location
extra_files model:
class ExtraFile < ActiveRecord::Base
belongs_to :archive
end
list.rhtml Relivent section:
<% @programs.each do |program| -%>
<tr>
<td><%= link_to program.program_name, program.file_location %>
<% program.extra_file.each do |file| -%>
<br><%= link_to file.name, file.location %>
<% end -%></td>
<td><%= program.author %></td>
<td><%= program.reference %></td>
<td><%= program.format %></td>
<td><%= program.description %></td>
</tr>
Now How to display the extra_files name and location in the edit.rhtml,
so they can be edited, and returned to the proper place in the database
is where I''m stuck...
I''m presuming the extra_file id would need to be associated with the
name and location fields somehow...
Here''s my edit.rhtml as it currently stands:
<html>
<head>
<title>Edit Program Listing</title>
</head>
<body>
<h1>Edit Program Listing</h1>
<form action="../update/<%= @program.id %>"
method="POST"">
<input id="archive_id" name="archive[id]"
size="30"
type="hidden" value="<%= @program.id %>"
/>
<p><b>Title</b><br>
<input id="program_name" name="archive[program_name]"
size="30"
type="text" value="<%= @program.program_name
%>" />
</p>
<p><b>Author</b><br>
<input id="program_author" name="archive[author]"
size="30" type="text"
value="<%= @program.author %>" />
</p>
<p><b>Format</b><br>
<input id="program_format" name="archive[format]"
size="30" type="text"
value="<%= @program.format %>" />
</p>
<p><b>Reference</b><br>
<input id="program_reference" name="archive[reference]"
size="30" type="text"
value="<%= @program.reference %>" />
</p>
<p><b>Release Date</b><br>
<input id="program_release_date"
name="archive[release_date]"
size="30" type="text"
value="<%= @program.release_date %>" />
</p>
<p><b>Description<b><br>
<textarea id="program_description"
name="archive[description]"
cols="40" rows="20"
wrap="virtual"><%= @program.description %></textarea>
</p>
<p><b>Misc (extended Description)</b><br>
<textarea id="program_misc" name="archive[misc]"
cols="40" rows="20"
wrap="virtual"><%= @program.misc %></textarea>
</p>
<p><b>Baud</b><br>
<select id="program_baud" name="archive[baud]">
<% [''2000'',''300'',''2000
BlueRam''].each do |baud| -%>
<option <%= ''selected'' if @program.baud == baud
%>><%= baud
%></option>
<% end -%>
</select>
</p>
<p><b>Archival Status</b><br>
<input id="program_arc_status"
name="archive[arc_status]"
size="30" type="text"
value="<%= @program.arc_status %>" />
</p>
<p><b>File Location<b><br>
<input id="program_file_location"
name="archive[file_location]"
size="30" type="text"
value="<%= @program.file_location %>" />
</p>
<p><b>Extra Ram</b><br>
Yes <input name="archive[ext_ram]"
type="radio" value="1"
<%= ''checked'' if @program.ext_ram == 1 %> />
No <input name="archive[ext_ram]"
type="radio" value="0"
<%= ''checked'' if @program.ext_ram == 0 %> />
</p>
<p><b>Category:</b><br>
<select name="archive[category_id]">
<% @categories.each do |category| -%>
<option value="<%= category.id %>" <%=
''selected'' if category.id
== @program.category_id %>><%= category.name %></option>
<% end -%>
</select></p>
<input type="submit" value="Update" />
</form>
<a href="/program_downloads/Archive/archive/show/<%= @program.id
%>">
Show </a> |
<%= link_to "Back", :action => "list" %>
</body>
</html>
I''m looking to add the list of existing extra files after the Category
drop-down.
I started with this:
<p><b>Extra Files</b><br>
<% program.extra_file.each do |file| -%>
<input id="...
... is where I realised I had NO concept how to ensure the data would
stay together properly...
Lance F. Squire
--
Posted via http://www.ruby-forum.com/.
Lance Squire
2006-Apr-07 20:14 UTC
[Rails] Re: Formatting form tags for children of parrent data?
After much hacking I''ve got this:
<p><b>Extra Files</b><br>
<% @program.extra_file.each do |file| -%>
Name: <input name="archive[extra_file[<%= file.id
%>]][name]"
type="text"
value="<%= file.name %>" />
Location: <input name="archive[extra_file[<%= file.id
%>]][location]"
type="text"
value="<%= file.location %>" /><br>
<% end -%>
Which looks promissing in the paramiters view on the error page, but
still dosen''t work...
"extra_file[2]"=>{"name"=>"test Extra
files2+", "location"=>"#"},
"extra_file[1]"=>{"name"=>"test Extra
files1+", "location"=>"#"},
--
Posted via http://www.ruby-forum.com/.