Forum: Ruby on Rails Partials problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul N. (Guest)
on 2007-07-10 19:11
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?
Jodi S. (Guest)
on 2007-07-10 19:21
(Received via mailing list)
Howdy Paul,

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

>    3.     @script = Script.new
>    4.     @script.script_metadatas.build
>    5.   end
>    6.


> 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/
Paul N. (Guest)
on 2007-07-11 13:04
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?
This topic is locked and can not be replied to.