帮助:向 Rails 中的现有帖子模型添加评级
我有一个简单的帖子模型,其中包含标题:字符串和评级:整数,我想添加对帖子进行评级的功能。到目前为止,
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有一个标准路线,则应
将其替换为:
这将为您的应用程序使用方法“put”创建路线“increase_post_path”。
If you have a standard routes saying
you should replace it with:
And this will create the route "increase_post_path" with method "put" for your app.
您需要将该操作添加到您的
routes.rb
文件中。有关详细信息,请参阅这篇出色的指南。You need to add the action to your
routes.rb
file. See this excellent guide for details.这是我的解决方案,如果有人感兴趣的话。谢谢你们的帮助。
如果您想要一些只适合您的东西,并且不会被滥用,那么这很有效。显然,为您的网站添加评级(让其他人可以无限次投票)只是自找麻烦。
我建议使用 Votefu,进行评分,甚至拥有用户业力。作者很友善地制作了一个示例应用。
Here's my solution, if anyone is interested. Thanks for your help guys.
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.