I’ve been working with a tutorial over on railsforum.com on creating
variable numbers of models in one form.
I have a script model that has one or many script_metadatas.
My code is the following:
-
scripts_controller.rb
- def new
-
@script = Script.new
-
@script.script_metadatas.build
- end
- def create3
-
@script = Script.new(params[:script])
-
params[:script_metadatas].each_value { |script_metadata|
@script.script_metadatas.build(script_metadata) }
10. if @script.save
11. redirect_to :action => ‘index’
12. else
13. render :action => ‘new’
14. end
15. end
16.
17. def add_script_metadata
18. @script_metadata = Script_metadata.new
19. end
20.
21. # scripts/new.rhtml
22. <% form_for :script, :url => { :action => ‘create3’ } do |f| %>
23.
Description: <%= f.text_field :description %>
24.
Script_metadatas
25.
26. <% @script.script_metadatas.each_with_index do |script_metadata,
index| %>
27. <%= render :partial => ‘script_metadata_fields’, :locals => {
:script_metadata => script_metadata, :index => index } %>
28. <% end %>
29.
30. <%= render :partial => ‘add_script_metadata_link’, :locals => {
:index => @script.script_metadatas.size } %>
31.
<%= submit_tag ‘Create Script’ %>
32. <% end %>
33.
34. #scripts/_script_metadata_fields.rhtml
35.
36. <% fields_for “script_metadatas[#{index}]”, script_metadata do |f|
%>
37.
38. <%= f.text_field :meta_key %>
39. <%= link_to_remote ‘remove’, :url => { :action =>
‘remove_script_metadata’, :index => index } %>
40.
41. <% end %>
42.
43.
44. # scripts/_add_script_metadata.rjs
45.
46. <%= link_to_remote ‘Add Another Script_metadata’, :url => {
:action => ‘add_script_metadata’, :index => index } %>
47.
48.
49. # scripts/add_script_metadata.rjs
50. page.insert_html :bottom, :script_metadatas, :partial =>
‘script_metadata_fields’,
51. :locals => { :script_metadata => @script_metadata, :index =>
params[:index] }
52.
53. page.replace :add_script_metadata_link, :partial =>
‘add_script_metadata_link’, :locals => { :index => (params[:index].to_i
The problem being, when I click on the button that calls
‘add_script_metadata.rjs’ nothing happens and I don’t get another row to
input another set of metadata.
Can anyone suggest why this is?