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:
1. # scripts_controller.rb
2. def new
3. @script = Script.new
4. @script.script_metadatas.build
5. end
6.
7. def create3
8. @script = Script.new(params[:script])
9. 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. <p>Description: <%= f.text_field :description %></p>
24. <h2>Script_metadatas</h2>
25. <div id="script_metadatas">
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. </div>
30. <%= render :partial =>
''add_script_metadata_link'', :locals => {
:index => @script.script_metadatas.size } %>
31. <p><%= submit_tag ''Create Script''
%></p>
32. <% end %>
33.
34. #scripts/_script_metadata_fields.rhtml
35. <div id="script_metadata_<%= index %>">
36. <% fields_for "script_metadatas[#{index}]", script_metadata
do |f|
%>
37. <p>
38. <%= f.text_field :meta_key %>
39. <%= link_to_remote ''remove'', :url => { :action
=>
''remove_script_metadata'', :index => index } %>
40. </p>
41. <% end %>
42. </div>
43.
44. # scripts/_add_script_metadata.rjs
45. <div id="add_script_metadata_link">
46. <%= link_to_remote ''Add Another Script_metadata'',
:url => {
:action => ''add_script_metadata'', :index => index }
%>
47. </div>
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
+ 1) }
54.
55. # scripts/remove_script_metadata.rjs
56. page["script_metadata_#{params[:index]}"].remove
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?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---