Rails 3 - 如何将语句从一个控制器打印到另一个控制器

发布于 2025-01-02 15:02:08 字数 1286 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我 2025-01-09 15:02:08

我开始写评论,但它太长了。

我没有使用过ajax分页,所以我不确定它应该如何工作。但是,你的代码让我感到困惑。

  1. 分页是否适用于标准 ArticlesController?如果即使这样也不起作用,那么也许您想从一个已知的工作示例开始,例如 RailsCast #174

  2. 看起来您正在从索引本身渲染索引(渲染@articles)。这真的有效吗?我希望它会因无限循环而崩溃或执行一些意外的操作。

  3. 也许这并不重要,但我只用过 will_paginate 这种方式。这会改变什么吗?

    Article.all.paginate(:page => params[:page], :per_page => params[:per_page])

  4. 再说一次,我没有使用 ajax 的经验,所以我无法帮助你直接地。但是,您是否先让它与标准 html 一起工作,然后在工作后逐步添加 ajax ?这是您可以使用的一种结构:(我更喜欢显式局部变量,但如果您愿意,您可以忽略它)。

articles/index.html.erb(与秒/index.html.erb相同)

...
<%= render :partial =>'articles/list', :locals => {:articles => @articles} %>
...

articles/_list.html.erb:

...
<%= articles.each do |article| %>
          <% render :partial =>'articles/article', :locals => {:article => article} %>
<% end %>
<%= will_paginate articles %>
...

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.

  1. 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.

  2. 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.

  3. 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])

  4. 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)

...
<%= render :partial =>'articles/list', :locals => {:articles => @articles} %>
...

articles/_list.html.erb:

...
<%= articles.each do |article| %>
          <% render :partial =>'articles/article', :locals => {:article => article} %>
<% end %>
<%= will_paginate articles %>
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文