【问题标题】:routing issue rails 3.1 thums_up and forem路由问题 rails 3.1 thums_up 和 forem
【发布时间】:2012-01-10 06:39:42
【问题描述】:

我正在尝试将 forem 与 thumbs_up 集成。我继承了 Forem Post 模型和控制器。

这是我的控制器:-

  class PostsController < Forem::PostsController
    def vote_up
    begin
      current_user.vote_for(@post = Post.find(params[:id]))
      render :nothing => true, :status => 200
    rescue ActiveRecord::RecordInvalid
      render :nothing => true, :status => 404
    end
  end
end

这是 Forem 的 Post Controller 的样子:-

module Forem
  class PostsController < Forem::ApplicationController
    before_filter :authenticate_forem_user
    before_filter :find_topic
    .
    .
    .
    .
private

    def find_topic
      @topic = Forem::Topic.find(params[:topic_id])
    end
  end
end

这是我的路线:-

 mount Forem::Engine, :at => "/forums"

resources :posts do
  member do
    post :vote_up
  end
end

这是我的看法:-

<%= link_to t('vote for this post!', :scope =>"forem.post"), main_app.vote_up_post_path(@post), :method => :post %>

这是我得到的错误:-

ActiveRecord::RecordNotFound in PostsController#vote_up

找不到没有 ID 的 Forem::Topic

可能是什么问题?

【问题讨论】:

  • current_user.vote_for 长什么样子?
  • @muistooshort current_user.vote_for 的样子是什么意思? vote_for 由 thumbs_up gem 提供
  • 看起来 current_user.vote_for 正在调用 Forem::Topic.find 而不给它一个 ID,这就是错误消息似乎告诉你的,我没有看到任何其他可能导致它的东西,所以我我想知道为什么vote_for 正在做一些不应该做的事情。
  • @muistooshort 这里是 thumbs_up gem 的链接github.com/brady8/thumbs_up/blob/master/lib/acts_as_voteable.rb,我认为它定义了 vote_for 操作。如果您有时间,请查看它
  • 我也从 forem::postscontroller 继承了我的帖子控制器,这可能是个问题吗?从 postcontroller 调用 Forem:Topic.find 的意义上说?它实际上做了一个 before_filter find_topic

标签: ruby-on-rails routing ruby-on-rails-3.1 gem


【解决方案1】:

你的问题是前置过滤器:

module Forem
  class PostsController < Forem::ApplicationController
    #...
    before_filter :find_topic
    #...
    def find_topic
      @topic = Forem::Topic.find(params[:topic_id])
    end

然后:

class PostsController < Forem::PostsController
  def vote_up
    #...

所以find_topic 将在vote_up 之前被调用,但vote_up 的路由不会有:topic_id;没有:topic_id 意味着find_topic 会这样做:

@topic = Forem::Topic.find(nil)

这就是你的错误的来源。

想到三个选项:

  1. vote_up 移至不继承自Forem::ApplicationController 的单独控制器类。
  2. skip_filter :find_topic, :only =&gt; :vote_up 添加到 PostsController。
  3. 调整路由和链接,在路由中得到:topic_id

如果投票不需要@topic,那么 (1) 或 (2) 将起作用,否则您将不得不使用 (3)。

【讨论】:

  • 我不知道如何继续使用选项 3,尝试了选项 1 和 2,但这似乎不起作用。我想@topic 将是必需的,因为我认为 forem 的架构是论坛 --> 主题 --> 发表任何想法?
  • @Dev:主题和帖子之间有关联吗?如果有,那么您可以从您的@post 获取您的@topic,而不是从params
  • 这是关联 has_many :posts, :dependent => :destroy, :order => "created_at ASC"
【解决方案2】:

在命令提示符中检查rake routes, 并且检查 id 应该是 post :vote_up 或 get:vote_up – ror_master

并在控制器中使用debugger! 并写params那里也许你会得到解决方案。

【讨论】:

  • 我认为你应该在路由中使用get:vote_up。请检查并验证。
猜你喜欢
  • 1970-01-01
  • 2013-11-06
  • 2020-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 1970-01-01
相关资源
最近更新 更多