Rails 3 - 如何将语句从一个控制器打印到另一个控制器
我有一个 ArticleController,其中有一个包含 Articles 表中所有项目的语句。该语句还使用 jQuery 进行分页。
我想在另一个控制器中使用此语句和分页来对数据库表中的项目进行语句,唯一的不同是另一个表。
我怎样才能做到这一点?我在其他控制器中尝试类似的操作:
<%= render :template => 'articles/index'%> #here is the Articles' statement
但是当我单击语句的下一页时,我只看到加载,但数据未加载 - 看起来其他页面未加载。
我做错了什么?
编辑: - 控制器:
ArticlesControllerarticles
def index
@articles = Article.all.per_page(8)
respond_to do |format|
format.html # index.html.erb
format.js
format.xml { render :xml => @articles }
end
end
/index.html.erbarticles
<div id="articles">
<%= render @articles %>
</div>
<%= will_paginate @articles %>
/index.js.erbarticles
$('#articles').append('<%= j render(@articles) %>');
/_article.html.erb
...just a printing of data...
第二个控制器 - SecondsController:
def index
@articles = Article.all.per_page(8)
end
秒/index .html.erb
<%= render :template => 'articles/index'%>
I have a ArticleController, in which is a statement with all items in the table Articles. The statement has also pagination with jQuery.
I would like to use this statement with pagination in an other controller for statement of items from DB table, the only different will be the other table.
How can I do that? I try in my other controller something like:
<%= render :template => 'articles/index'%> #here is the Articles' statement
But when I click on the next page of statement, I see only loading, but the data are not loaded - looks like the other pages are not loaded.
What I am doing wrong?
EDIT: - controllers:
ArticlesController
def index
@articles = Article.all.per_page(8)
respond_to do |format|
format.html # index.html.erb
format.js
format.xml { render :xml => @articles }
end
end
articles/index.html.erb
<div id="articles">
<%= render @articles %>
</div>
<%= will_paginate @articles %>
articles/index.js.erb
$('#articles').append('<%= j render(@articles) %>');
articles/_article.html.erb
...just a printing of data...
The second controller - SecondsController:
def index
@articles = Article.all.per_page(8)
end
seconds/index.html.erb
<%= render :template => 'articles/index'%>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我开始写评论,但它太长了。
我没有使用过ajax分页,所以我不确定它应该如何工作。但是,你的代码让我感到困惑。
分页是否适用于标准 ArticlesController?如果即使这样也不起作用,那么也许您想从一个已知的工作示例开始,例如 RailsCast #174。
看起来您正在从索引本身渲染索引(渲染@articles)。这真的有效吗?我希望它会因无限循环而崩溃或执行一些意外的操作。
也许这并不重要,但我只用过 will_paginate 这种方式。这会改变什么吗?
Article.all.paginate(:page => params[:page], :per_page => params[:per_page])
再说一次,我没有使用 ajax 的经验,所以我无法帮助你直接地。但是,您是否先让它与标准 html 一起工作,然后在工作后逐步添加 ajax ?这是您可以使用的一种结构:(我更喜欢显式局部变量,但如果您愿意,您可以忽略它)。
articles/index.html.erb(与秒/index.html.erb相同)
articles/_list.html.erb:
I started to write a comment, but it got too long.
I haven't used the ajax pagination so I am not sure how it should work. However, your code confuses me.
Does the pagination work on the standard ArticlesController? If even that doesn't work, then maybe you want to start from a known working example like RailsCast #174.
It looks like you are rendering the index (render @articles) from within the index itself. Does that actually work? I would expect it to crash with an infinite loop or do something unexpected.
Maybe it doesn't matter, but I've only used will_paginate this way. Does this change anything?
Article.all.paginate(:page => params[:page], :per_page => params[:per_page])
Again, I don't have experience working with ajax here so I can't help you directly. However, did you get it working with standard html first and then progressively add ajax after it works? Here is one structure you could use: (I prefer explicit locals, but you can probably ignore that if you want).
articles/index.html.erb (same for seconds/index.html.erb)
articles/_list.html.erb: