2013-08-02 57 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