开发者

A dry way to build page with multiple blocks (from different controllers) and ajax in rails

开发者 https://www.devze.com 2023-04-11 23:41 出处:网络
Main page of my website consists of multiple blocks such as last offers, last news, last articles and so on. Each block has controls like \"show more\" or \"refresh\" so that when user clicks, the blo

Main page of my website consists of multiple blocks such as last offers, last news, last articles and so on. Each block has controls like "show more" or "refresh" so that when user clicks, the block should refresh or update it's own content through ajax. Also when a page is loaded the first time, it should already be filled with content (for disabled javascript and search engines).

What's the best, dry, rails-way to build such structure?

That's In my articles_controller.rb:

def line
  @articles = Article.filter(params)
  respond_to do |format|
    format.html { render :layout => false, :partial => "articles/line.html", :locals => {:articles => @articles} }
    format.json { render :json => { "html" => render_to_string(:partial => "articles/line.html", :locals => {:articles => @articles}) } }
  end
end

When I call /articles/line through ajax it sends json that contains cut of html-code, and all that remains is to append it to a specified div. This works. When I call /articles/line directly it also works and displays required partial of html-code.

But what should I write in my main_controller (it should collect and render all blocks from different controllers)? Where should I write some blocks settings (for example, quantity of records and offset, they required both in static and ajax cases)?

Most convenient开发者_运维技巧 for me is to set everything up right in the view template of main page, like that:

<div id="last-news" data-params="{offset:0,limit:5}">
  <%= render #here we get html from :controller => news, :action => list, :params => get them from parent div %>
</div>
<div id="last-articles" data-params="{offset:0,limit:10}">
  <%= render #here we get html from :controller => articles, :action => list, :params => get them from parent div %>
</div>

But that doesn't look like MVC.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号