【问题标题】:acts_as_votable redirecting when trying to vote up/vote down尝试投票赞成/反对票时的acts_as_votable重定向
【发布时间】:2013-10-24 05:53:24
【问题描述】:

我对acts_as_votable gem 有疑问。我有一个简单的论坛应用程序,我希望有功能来对帖子进行投票。我用于这个acts_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" 怎么了?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

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

    redirect_to :back

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

    redirect_back(fallback_location: root_path)

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 它不工作。我有以下错误:#<0x007f3ebd021068>
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2021-06-19
      • 1970-01-01
      • 2018-07-08
      • 2012-12-15
      • 2018-06-07
      • 1970-01-01
      相关资源
      最近更新 更多