在 Rails 中使用多个块(来自不同控制器)和 Ajax 构建页面的干法
我网站的主页由多个块组成,例如最新报价、最新新闻、最新文章等。每个块都有“显示更多”或“刷新”等控件,以便当用户单击时,块应该通过 ajax 刷新或更新其自己的内容。 此外,当第一次加载页面时,它应该已经填充了内容(对于禁用的 javascript 和搜索引擎)。
建造这种结构的最佳干燥铁路方式是什么?
那是在我的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
当我通过ajax调用/articles/line时,它会发送包含html代码片段的json,剩下的就是将其附加到指定的div。这有效。当我直接调用 /articles/line 时,它也可以工作并显示所需的部分 html 代码。
但是我应该在 main_controller 中写什么(它应该收集并渲染来自不同控制器的所有块)?我应该在哪里编写一些块设置(例如,记录数量和偏移量,它们在静态和ajax情况下都需要)?
对我来说最方便的是在主页的视图模板中正确设置所有内容,如下所示:
<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>
但这看起来不像 MVC。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论