带 Kaminari 分页和远程形式的 Rails

发布于 2024-12-29 12:07:00 字数 1019 浏览 1 评论 0原文

我正在开发一个使用 Rails 3.1.3 和 kaminari 0.13.0 的项目。我有两个模型文章和评论。文章有很多评论。我的控制器是 ArticlesController,它基本上可以完成除创建评论之外的所有操作。这是由 CommentsController 处理的。在我看来,对于“articles#show”,我有一个表格来创建新评论。我希望远程提交此表单(:remote => true)。我在它下面有一个评论列表,用 kaminari 分页。当然,我希望刷新该列表。但是,当评论提交到“comments#create”时,重新呈现时分页链接会变得不正确。我尝试添加 <%= paginate @comments, :params => {:控制器=> "文章", :action => 'show' } %> 让它们指向正确的网址,但这并没有解决我的问题。重新呈现列表时,我收到:

ActionView::Template::Error (No route matches 
{:utf8=>"✓",:authenticity_token=>"5VJqDLifXX0O/ttq9rNY0POUbsN678I6Au2iL2Qcv7w=",
 :comment=>{"name"=>"4004053","content"=>"asdf"},:commit=>"Create Comment",
 :action=>"show",:controller=>"articles",:article_id=>"4f24fbb4f4b7b863a4000009",
 :page=>nil}):

我可以通过添加额外的属性来分页并使其 <%= paginate @comments, :custom_url => 来解决此问题“/articles/#{@article._id}”%>。然后我可以更改 kaminari 的视图,将 url 设置为我的 custom_url(如果存在)。不过我不太喜欢这个主意。

我错过了什么吗?有更好的方法吗?

先感谢您!

I am developing a project with rails 3.1.3 and kaminari 0.13.0. I have two models Article and Comment. Article has many Comments. My controllers are ArticlesController which basically does everything except for creating comments. This is handled by CommentsController. In my view for 'articles#show' I have a form to crate new comments. I want this form to be submitted remotely (:remote => true). I have a list of comments right bellow it which is paginated with kaminari. Naturally, I want the list to be refreshed. However as the comment is submitted to 'comments#create', the pagination links become incorrect when rerendered. I tried to add <%= paginate @comments, :params => { :controller => "articles", :action => 'show' } %> to make them point to the right url, however this didn't solve my problem. When the list is rerendered, I receive:

ActionView::Template::Error (No route matches 
{:utf8=>"✓",:authenticity_token=>"5VJqDLifXX0O/ttq9rNY0POUbsN678I6Au2iL2Qcv7w=",
 :comment=>{"name"=>"4004053","content"=>"asdf"},:commit=>"Create Comment",
 :action=>"show",:controller=>"articles",:article_id=>"4f24fbb4f4b7b863a4000009",
 :page=>nil}):

I can solve this by adding extra attribute to paginate and make it <%= paginate @comments, :custom_url => "/articles/#{@article._id}" %>. Then I can change kaminari's views to set the url to my custom_url if it is there. However I don't like very much the idea.

Am I missing something? Is there a better way to do this?

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

夏雨凉 2025-01-05 12:07:01

事实证明

<%= paginate @comments, :params => { :controller => "articles", :action => 'show' } %>

几乎可行,但是我需要添加 :id => @article.id 所以它变成了

<%= paginate @comments, :params => { :controller => "articles", :action => 'show', :id => article.id } %>

相当愚蠢的错误,我现在明白了。我对路由的理解不够。

It turned out that

<%= paginate @comments, :params => { :controller => "articles", :action => 'show' } %>

almost worked, however I needed to add :id => @article.id so it became

<%= paginate @comments, :params => { :controller => "articles", :action => 'show', :id => article.id } %>

Quite stupid mistake, I see now. I didn't understand the routing enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文