Description
I have the following working code.
-
View with list of links:
<%= div_for feed do %> <%= link_to feed.name, { :controller => 'Feeds', :action => "get_posts", :id => feed.id }, :remote => true %> <% end %> -
Controller which works when user clicks the link:
def get_posts @result = Feed.find(params[:id]).generate_html_table respond_to do | format | format.js {render :layout => false} end end -
Method which generates HTML:
def generate_html_table r = "<table>" self.posts.order("published desc").each { |p| link = '<a href=' + p.link + '>' + p.title + '</a>' date = ' ' + p.published.to_s r += '<tr><td>' + link + '</td><td>' + date + '</td></tr>' } r + '</table>' end -
And get_posts.js.erb:
$( "#in" ).html("<%= raw @result %>");
Users clicks the link, generate_html_table generates HTML code which inserted into #id div.
Question
I want to change generate_html_table with partials. How can I get it?
How did I try it
-
I have created file views/feeds/_posts.html.erb with:
some static text -
I have edit my get_posts.js.erb as:
$( "#in" ).html("<%= escape_javascript(render :partial => 'posts' )%>");
But even now, with static text it doesn’t work.
It looks like you have view code in the controller, you should get rid of the
generate_html_tablemethod and leave that to theget_posts.js.erb, when you are putting code that belongs to a view so clearly you know you are going offtrack.The
get_posts.js.erbyou have in the second part of the question seems right, I don’t see a#inin your previous view code where that code would be inserted, have you checked calling the ajax URL directly to discard a problem in the ajax part? (like not having loaded thejquery-ujsor whatever unobtrusive javascript platform you are using). I also don’t see any code that is handling theremote, so the javascript that is returning from the call is probably not being used.My recommendation would be:
generate_html_tableself.posts.order("published desc")) in the controller, that does belong in the controllerajax:successcallback of your button to insert the html in#in