帮助:向 Rails 中的现有帖子模型添加评级

发布于 2024-08-18 21:23:18 字数 460 浏览 2 评论 0原文

我有一个简单的帖子模型,其中包含标题:字符串和评级:整数,我想添加对帖子进行评级的功能。到目前为止,

#Post controller   
def increase
 @post = Post.find(params[:id])
 @post.increment! :rating
 flash[:notice] = "Thanks for your rating."
 redirect_to @post
end

#Post show
<%= link_to "Rating", increase_post_path %>

#Routes
map.resources :posts, :member => { :increase => :put }

当我点击评级时,我得到了未知的操作。当我添加@post.increment 时,我可以提高评级! :评级要更新,但当我创建自己的方法时则不会。有什么建议吗?

I've a simple posts model with title:string and rating:integer and I want to add the ability to rate the posts. So far I have

#Post controller   
def increase
 @post = Post.find(params[:id])
 @post.increment! :rating
 flash[:notice] = "Thanks for your rating."
 redirect_to @post
end

#Post show
<%= link_to "Rating", increase_post_path %>

#Routes
map.resources :posts, :member => { :increase => :put }

When I click on rating, I get unknown action. I'm able to increase the rating when I add @post.increment! :rating to update, but not when I create my own method. Any suggetions?

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

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

发布评论

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

评论(3

下雨或天晴 2024-08-25 21:23:18

如果您有一个标准路线,则应

map.resources :posts

将其替换为:

map.resources :posts, :member => { :increase => :put }

这将为您的应用程序使用方法“put”创建路线“increase_post_path”。

If you have a standard routes saying

map.resources :posts

you should replace it with:

map.resources :posts, :member => { :increase => :put }

And this will create the route "increase_post_path" with method "put" for your app.

旧人哭 2024-08-25 21:23:18

您需要将该操作添加到您的routes.rb 文件中。有关详细信息,请参阅这篇出色的指南

You need to add the action to your routes.rb file. See this excellent guide for details.

夜声 2024-08-25 21:23:18

这是我的解决方案,如果有人感兴趣的话。谢谢你们的帮助。

#Views
<%= link_to post.rating, increase_post_path(post) %>

#Controller
def increase
  @post = Post.find(params[:id]).increment!(:rating)
  flash[:notice] = "Thanks for rating"
  redirect_to posts_url
end

#Routes
map.resources :posts, :member => { :increase => :put }

如果您想要一些只适合您的东西,并且不会被滥用,那么这很有效。显然,为您的网站添加评级(让其他人可以无限次投票)只是自找麻烦。

我建议使用 Votefu,进行评分,甚至拥有用户业力。作者很友善地制作了一个示例应用

Here's my solution, if anyone is interested. Thanks for your help guys.

#Views
<%= link_to post.rating, increase_post_path(post) %>

#Controller
def increase
  @post = Post.find(params[:id]).increment!(:rating)
  flash[:notice] = "Thanks for rating"
  redirect_to posts_url
end

#Routes
map.resources :posts, :member => { :increase => :put }

This works if you want something just for you, something that won't be abused. Obviously adding rating to your websites where others can vote up unlimited times is just asking for trouble.

I suggest using Votefu, does ratings and even has user karma. The author was nice enough to make an example app.

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