2013-10-16 59 views
0

我與acts_as_votable寶石的問題。我有簡單的論壇應用程序,我想要功能來投票上下。我用這個acts_as_votable寶石。我已經在帖子里加入控制器兩種方法:acts_as_votable嘗試投票時重定向/投下來

def upvote 
    @post = post.find(params[:id]) 
    @post.liked_by current_user 
    redirect_to forum_topic_path(@post.topic.forum, @post.topic) 
    end 

    def downvote 
    @post = post.find(params[:id]) 
    @post.downvote_from current_user 
    redirect_to forum_topic_path(@post.topic.forum, @post.topic) 
    end 

我的路線是:

resources :topics, except: :index do 
    resources :posts do 
     member do 
     put "like", to: "posts#upvote" 
     put "dislike", to: "posts#downvote" 
     end 
    end 
    end 

在我的主題秀動作視圖我有以下鏈接:

= link_to "Upvote", like_topic_post_path(post.topic.id,post.id, method: :put) 
= link_to "Downvote", dislike_topic_post_path(post.topic.id,post.id, method: :put) 

當我'm試圖點擊upvote或downvote我已被重定向到: http://localhost:3000/topics/104/posts/55/like?method=put,我有以下錯誤: 沒有路由匹配[GET]「/ topics/104/posts/55/like「 有什麼不對?

回答

0

嘗試 「GET」 方法,而不是 「PUT」,如下圖所示,

resources :topics, except: :index do 
    resources :posts do 
     member do 
     get "like", to: "posts#upvote" 
     get "dislike", to: "posts#downvote" 
     end 
    end 
    end 
+0

這不工作ING。我有以下錯誤:未定義的局部變量或方法'後'爲#。錯誤是在線: @post = post.find(params [:id]) –

+0

表名的第一個字母應該像上限一樣。Post.find – visnu

+0

現在一切正常,但當我點擊upvote或downvote它無法正常工作。發帖仍然有0票 –

0

我會改變對重定向到PostsController代碼:如果您在使用Rails

redirect_to :back

5這是更好的:

redirect_back(fallback_location: root_path)