2013-08-02 58 views
0

我的投票方式有问题,我的投票方式有问题。我是RoR的一名成员,正在等待你的建议。在Ruby on Rails上使用Mongoid投票

我得到的错误是:

的ActionController :: RoutingError在/职位/ 51f7d1279fefa5405a000003没有 路由匹配{:控制器=> “意见”:动作=> “投票(1)”, :类=> “post__button - 编辑”}

我的代码:

comment.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 

comment_controller.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 

的routes.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 

进出口等待您的帮助:)

回答

0

改变什么是a这里?我猜这是投票方向

您必须从vote行动中移除参数a,并通过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 
+0

改变,仍然是同样的问题。 – user2646329

+0

我需要补充的是,该方法(投票)用于'post'视图(app/views/posts/show.haml)中: @ post.comments.each do | comment | 。 。 。 %td = link_to“YES”,controller:“comments”,action:“vote(comment,1)”,class:'post__button - edit' – user2646329

+0

我在回答中添加了更多描述。 – ck3g

0

尝试这样

Easyblog::Application.routes.draw do 

    resources :posts do 
    resources :comments do 
     match :vote 
    end 
    member do 
     post :mark_archived 
    end 
    end 
end 
+0

nope,仍然是同样的问题。 – user2646329

0

你可以尝试这样的事情在您选择的路线

resources :posts do 
    resources :comments do 
    member do 
     post 'vote' 
    end 
end 
member do 
    post :mark_archived 
end