[Help] How to render or replace a partial page having <script> tag?

My problem is quite complicated.

In my project, I have to use static

  • However, the section above is not static, I have to write a
    helper to dynamically generate it. so it becomes to

    1. <%= generate_tree_view(:id=>1) ## this will generate the
    section and insert them in the page 2.
    But, right now, my problem is I have to call this tree view page by ajax like this 1. ## view file show.erb.html 2. ... 3. <%= link_to_remote 'see_tree', :url=>{:action=>'view_tree', :id=>1} > 4.
    5. ... 1. ## controller file tree_controller.rb 2. ... 3. def view_tree 4. ### get some parameters, say 'tree_parameter' array 5. render :update do |page| 6. page.replace_html 'view_tree', :partial=>'view_tree', :object=>tree_parameter 7. end 8. end 9. ... 1. ## partial page _view_tree.erb.html 2. <%= generate_tree_view(:tree_parameter=>view_tree) 3.
    I thought this should work because it actually worked if I directly use the generate_tree_view help in the show.erb.html. But however, when the partial page is called by ajax, the section which is generated by generate_tree_view didn't show up in the page. The final page only shows this: 1. ## view file show.erb.html, saw by firebug 'cause it is the only way I can see a rendered page. 2. ... 3.
    4.
    5.
    6. ... It seems they got executed by the javascript call instead of just display it. So, what should I do if I just want to inject a section in a rendered page. I'm not sure whether you have already got my idea, but I really appreciate it if you help me solve this problem. Thank you.

    I don’t use RJS although I have used rails to generate javascript
    dynamically. I tend to prefer to see the javascript that’s being
    executed so that when things go wrong I know exactly why. Anyway,
    someone else reading this thread might have a quick answer whereas I
    don’t, but …

    On Oct 25, 3:53 pm, boblu [email protected] wrote:

    1. get some parameters, say ‘tree_parameter’ array

    2. render :update do |page|

    3. page.replace_html
      

    ‘view_tree’, :partial=>‘view_tree’, :object=>tree_parameter
    7. end
    8. end
    9. …

    1. partial page _view_tree.erb.html

    2. <%= generate_tree_view(:tree_parameter=>view_tree)

    There are no quotes around ‘view_tree’ above. Is that right?

    Check the javascript console (ctrl+shift+J in firefox) for javascript
    errors that your function may be making.

    Instead of RJS you could just try rendering the partial (that’s just
    my preference - it might be easier to see what’s going wrong).
    render :partial => ‘view_tree’ ,
    :locals => { :your_var => ‘your val’ … }
    (_view_tree.html.erb)
    And in your link_to_remote, I think you’ll need to include an :update
    option with the id of the element whose inner html you are updating.
    See the docs
    http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html

    Any script tag in the partial will probably be eval’d by the
    javascript interpreter. (The link_to_remote should generate an
    Ajax.Updater with the evalScripts:true option which you can check for
    just to make sure).

  • It seems they got executed by the javascript call instead of just
    display it.

    So, what should I do if I just want to inject a section in a
    rendered page.

    Prototype will remove script tags whether it is set to process them or
    not so you will likely not see them in firebug after the update (which
    is probably a good thing). (I’m referring specifically to prototype’s
    Ajax.Updater)

    Also check your output in your functional tests for this controller:

    Check the partial _element3_1.html.erb.

    def test_1a
    xml_http_request :post,‘view_tree’,params
    assert_tag :tag => ‘script’ , :content => /alert/
    end


    Daniel B.