0

我的评论投票方法有问题。我是 RoR 的初学者,正在等待您的建议。

我得到的错误是:

ActionController::RoutingError at /posts/51f7d1279fefa5405a000003 没有路由匹配 {:controller=>"comments", :action=>"vote(1)", :class=>"post__button--edit"}

我的代码:

评论.rb

class Comment
  include Mongoid::Document

  field :name, type: String
  field :email, type: String
  field :body, type: String
  field :up_vote, type: Integer, default: "0"
  field :down_vote, type: Integer, default: "0"
  belongs_to :post

  validates_presence_of :name, :email, :body

  def self.add_up_vote
    self.increment(:up_vote, 1)
  end

  def self.add_down_vote
    self.decrement(:down_vote, 1)
  end
end

注释控制器.rb

.
.
.
def vote(a)
    @comment = Comment.find(params[:comment_id])
    @post = Post.find(params[:post_id])

    if a == 1
      comment.add_up_vote
      redirect_to @post
    elsif a == -1
      comment.add_down_vote
      redirect_to @post
    else
      redirect_to @post
    end

  end

路线.rb

Easyblog::Application.routes.draw do

  authenticated :user do
    root :to => 'home#index'
  end
  root :to => "home#index"
  devise_for :users
  resources :users
  resources :posts do
    resources :comments
    member do
      post :mark_archived
    end
  end
end

我在等你的帮助:)

4

4 回答 4

0

试试这样改

Easyblog::Application.routes.draw do

  resources :posts do
    resources :comments do
      match :vote
    end
    member do
      post :mark_archived
    end
  end
end
于 2013-08-02T14:48:33.130 回答
0

这是什么a?我想这是投票方向

您必须avote操作中删除参数并通过params链接路径传递方向。

狐狸示例:

vote_comment_path(@comment.id, dir: 1) # or dir: -1

不仅如此,没有vote采取行动的途径。你可以这样描述它:

resources :comments do
  put :vote, as: :member
end

upd我建议您阅读以下指南http://guides.rubyonrails.org/routing.html

action在您的路径中无效。你的链接应该看起来像

= link_to 'Yes', vote_comment_path(comment, dir: 1), method: :put

vote_comment_path可以不同,您可以通过rake routes命令检查它:

$ rake routes
于 2013-08-02T14:49:07.450 回答
0

你可以在你的路线中尝试这样的事情

resources :posts do
  resources :comments do
    member do
      post 'vote'
  end
end 
 member do
  post :mark_archived
end
于 2013-08-03T19:41:55.627 回答
0

无需建立自己的投票系统。看看voteable_mongo gem

于 2013-08-03T19:49:33.497 回答