Topic: :collection in :partial in .rjs

Hi,
Is it possible to replace_html with :collection provided for :partial?
I tryed in .rjs

page.replace_html 'main', :partial => 'articles/article', :collection => @articles, :locals => { :user => @user }

and in partial
<div id="article">
    <div id="articleHeader"><%= link_to article.title,  article_url(user, article) %> by
        <%= link_to user.name, user_path(user) %>
       
        <% if_owner user do %>
        <div style="float:right;">
            <%= link_to 'edit', edit_article_path(user, article) %>
            <%= link_to 'destroy', article_path(user, article), :confirm => 'Are you sure?', :method => :delete %>
        </div>
        <% end %>
    </div>

<p>
  <%=h article.body %>
</p>

</div>


in my helper method i create link_to_remote like this
link_to_remote( tag.name + " ", {:url => user_tag_articles_url(user.id, tag.id), :update => "main", :method => :get}, {:class => link_class})

and in controller for .rjs
    def articles_for_user_tag
        @user = User.find(params[:user])
        @articles = @user.tags.find(params[:tag]).articles
        respond_to do |format|
            format.js
        end
    end

i also tryed passing :object => @articles to partial which loops through
But the content in div is replaced with something lke this:
try { Element.update("main", "
\n
dflsjlfkjs by\n sarge\n \n \n
\n edit\n destroy\n
\n \n
\n\n
\n jlfjls\n

\n\n
"); } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('Element.update(\"main\", \"
\\n
dflsjlfkjs by\\n sarge\\n \\n \\n
\\n edit\\n destroy\\n
\\n \\n
\\n\\n
\\n jlfjls\\n

\\n\\n
\");'); throw e }

iChat ahoge_@mac.com

Re: :collection in :partial in .rjs

only thing i notice from looking over your code is:

you specified the :update option in linkt_to_remote. This means that the client expects to receive pure HTML from the server to update the tag specified by :update => 'main' with that HTML.

RJS doesn't return pure HTML, it returns Javascript which contains HTML, and that Javascript has to be executed by the client.

See the conflict? pure HTML vs. JS containing HTML

2 Options:

1) Try to remove the :update option from the link_to_remote You should see it's unnessessary as you specify the tag to replace in ('main') in the RJS, so there's no need to specify it in the link_to_remote

2) leave tzhe :update option as it is, but don't render an RJS, and just render :partial => 'wahtever' in the controller like you would do with a normal page refresh.

If your RJS is only this one line, go with 2).
If you do more than one RJS action, go with 1).

Last edited by Duplex (2007-10-13 21:58:15)

Re: :collection in :partial in .rjs

Thanks Duplex!

iChat ahoge_@mac.com