replace_html in Ruby on Rails generates TypeError
I'm having a problem similar to but not quite the same as one I've seen discussed a lot. Just want to put this out here in case anyone has any insight on this particular situation. I have a series of select boxes and after the last of these has been selected, I want to update a text_field with some text based on the above.
I have the following code in my view (actually in a partial) for the select box:
<%= f.collection_select :xyz_id, @xyzs, :id, :display_name,
{ :prompt => "Select a XYZ..." },
{ :onchange => "#{remote_function(:url => {:action => "update_text_field"},
:with => "'abc_id='+$('#foo_abc_id').val()")}",
:class => "blah" } %>
开发者_JAVA技巧The update_text_field
method in the controller does its work and calls replace_html
like so:
some_info = foo.bar.first(:order => "id DESC").name
render :update do |page|
page.replace_html 'myDivId', :partial => "shared/my_partial", :locals => { :some_info => some_info }
end
Finally, my_partial
looks like this:
<%= text_field_tag :special_info, some_info, :size => 20, :readonly => "readonly" %>
The partial that contains this partial has this:
<div id="myDivId"><%= render :partial => 'shared/my_partial',
:locals => { :some_info => some_info } %></div>
When run, all works well until the replace_html
which throws an RJS error:
RJS error:
TypeError: Result of expression 'element.getElementsByTagName' [undefined] is not a function
Followed by:
Element.update("myDivId",
"<input id=\"special_info\"
name=\"special_info\"
readonly=\"readonly\" size=\"20\"
type=\"text\" value=\"The right text\"
/>");
Any ideas on the cause of this error?
I have this working now.
Lessons:
- Use render :partial instead of render :update in the controller
- Use unobtrusive JS to issue a get request to the controller
- Use :remote => true on the form
- Don't look for solutions or experiment at night when you've been working for the past 12+ hours
The above list definitely seems to be "sufficient", but I'm not sure if everything in there is "necessary", but I'm going to move on using the above list for all of the other dynamic input fields on my form.
精彩评论