Partials problem


#1

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. def create3
  7. @script = Script.new(params[:script])
    
  8. 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
    1. }
    1. scripts/remove_script_metadata.rjs

    2. 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?


#2

Howdy Paul,

On 10-Jul-07, at 11:11 AM, Paul Noga wrote:

  1. @script = Script.new
    
  2. @script.script_metadatas.build
    
  3. end

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?

This is the kinda problem where the right debugging tool will pay
huge dividends.

(if you haven’t) pickup firebug[1] - using it you can watch the http
request/response; then tail your development log. These 2 bits of
info will tell you more symptoms. You’ll likely solve your problem,
if not you’ll have more info for us to help you out.

Jodi

[1] http://www.getfirebug.com/


#3

Thanks for the reply, Jodi.

I’ve just downloaded and installed the firebug plugin. Having never used
it could you point me in the right direction as to under which tab I
should be looking?

P.S. The script is now working as it should - in IE only…

I have one line for script name, description, path.

Then underneath it one line for metadata name and metadata value with an
‘add another’ button to add more metadata name/value pairs.

When multiple name/value pairs are input, IE stores all of them in the
database. Wheras Firefox only stores the very first pair. Can you
suggest why this is?