繁体   English   中英

当尝试投票/否决时,acts_as_votable重定向

[英]acts_as_votable redirecting when trying to vote up/vote down

我有act_as_votable gem的问题。 我有一个简单的论坛应用程序,并且希望具有对帖子进行投票和投票的功能。 我用于这个act_as_votable gem。 我向帖子控制器添加了两种方法:

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)

当我尝试单击upvote或downvote时,我已重定向至: http://localhost:3000/topics/104/posts/55/like?method=put并且出现以下错误:没有与[GET]匹配的路由/ topics / 104 / posts / 55 / like“怎么了?

尝试使用“ 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

我将PostsController代码更改为重定向到:

redirect_to :back

如果您使用的是Rails 5,则更好:

redirect_back(fallback_location: root_path)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM