Kaminari 分页与 fields_for
我有一个公司模型的表格:
<%= form_for(@company) do |f| %>
我还有一个 fields_for 部分来编辑导入关系:
<%= f.fields_for(:imports) do |builder| %>
Company has_many :imports 和 Import own_to :company
我想使用 Kaminari 进行分页,但问题是,Kaminari 需要从控制器返回一个页面对象,如下所示:
@imports = Import.where(:company_id => current_user.company.id).page(params[:page]).per(50)
这允许我使用 Kaminari 的分页方法:
<%= paginate @imports %>
这有效,并在我的页面上显示页面链接然而,它们显然没有链接到我的 fields_for 块。
我的问题是,如何使用 fields_for 块完成分页? 我需要允许用户编辑导入模型的列表,并且可能会有太多的模型无法容纳在一页上,这就是我尝试分页的原因。基本上我正在尝试为用户创建类似电子表格的体验。
我不需要使用 Kaminari,但我使用的是 Rails 3.1,它似乎是流行的选择。
感谢您对此的任何帮助。
I have a form for a company model:
<%= form_for(@company) do |f| %>
I also have a fields_for section to edit the imports relation:
<%= f.fields_for(:imports) do |builder| %>
Company has_many :imports
and
Import belongs_to :company
I want to use Kaminari for pagination, but the problem is, Kaminari needs a page object returned from the controller like such:
@imports = Import.where(:company_id => current_user.company.id).page(params[:page]).per(50)
This allows me to use the paginate method from Kaminari:
<%= paginate @imports %>
That works, and displays the page links on my form, however, they are obviously not linked to my fields_for block.
My question is, how can I accomplish pagination with a fields_for block?
I need to allow the user to edit a list of Import models, and there will probably be too many to fit on one page which is why I'm trying to paginate. Basically I'm trying to create a spreadsheet like experience for the user.
I don't need to use Kaminari, but I'm on Rails 3.1 and it seemed to be the popular choice.
Thanks for any help on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您也可以将“fields_for”与对象集合一起使用。
所以你可以做
如果这回答了你的问题那么你就完成了!然而,如果你希望它成为一个像考验一样的“电子表格”,那么也许就不那么重要了。
问题是,如果您每次转到新页面时都这样做,您将丢失所有已编辑的
导入
。这样做可能更简单:
fields_for
并隐藏它们。这样,当提交更改时,它将传递所有
导入
及其更改,而不仅仅是当前页面。You can use 'fields_for` with a collection of objects as well.
So you can do
If that answers your question then you're done! However if you want it to be a 'spreadsheet' like ordeal then maybe not so much.
The problem being that if you do that each time you go to a new page you will lose all your edited
imports
.It may be simpler to do this:
fields_for
and hide them.That way when the submit the changes it will pass all the
imports
and their changes instead of just the current page.