ruby-on-rails - Rails url 助手在 namespace Controller 中不可用

标签 ruby-on-rails ruby

我有一个 namespace Controller ,它来 self 正在创建的 gem,似乎没有来自路由的 *_path*_url 助手在尝试加载其中一个 Action 时可用于任何其他模型。我似乎无法弄清楚为什么它们在这个 Controller 中不可用。

Controller

class Surveyor::AttemptsController < ApplicationController
 load_and_authorize_resource

 before_filter :load_active_survey

 def new
  @participant = current_user

  unless @survey.nil?
   @attempt = @survey.attempts.new
   @attempt.answers.build
  end
 end

 def create
  @attempt = @survey.attempts.new(params[:survey_attempt])
  @attempt.participant = current_user

  if @attempt.valid? && @attempt.save
    redirect_to view_context.new_attempt, alert: I18n.t("attempts_controller.#{action_name}")
   else
    render :action => :new
   end
  end

  private

  def load_active_survey
   @survey =  Surveyor::Survey.active.first
  end
end

路线

...
namespace :surveyor do
  resources :attempts, :only => [:new, :create]
end
...

除了这些,其他所有路线都很好。我收到错误:

undefined method `team_path' for #<#<Class:0x0000010f330bc0>:0x00000107500840>

Team 是另一个模型,我有一个部分调用它,这恰好是对 *_path 的第一次调用,如果我更改它,它只会继续在渲染过程中失败。这是 html:

<li><a href="<%= team_path(current_user.team) %>">My Team</a></li>

current_user 确实已定义且可用

有什么想法吗?我进行了很好的搜索,但一切都与人们没有在他们的 Controller 方法中为带有表单的页面定义 @variable 相关,但我不是这种情况。

编辑(解决方案):

由于某些原因,当我将 helper Rails.application.routes.url_helpers 添加到我的 Controller 时一切正常。这似乎更像是 Rails 中的一个错误,因为我不应该这样做,但是哦,好吧。

最佳答案

尝试使用:

app.team_path

访问您通常不会访问的路径助手(例如在控制台中)。

令人费解的是,仅仅为 Controller 命名空间就能做到这一点。我会在某个时候尝试重新创建。

关于ruby-on-rails - Rails url 助手在 namespace Controller 中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25013806/

相关文章:

mysql - 尝试使用 Ruby 2 在 Windows 8 上安装 mysql2 gem

ruby-on-rails - 对这种方法中 self 的含义感到困惑

ruby-on-rails - 一个很好的 gem 排名算法

javascript - Ruby on Rails : Validate a user has selected a checkbox

ruby - NLP对句子的内容进行分类/标注(需要Ruby绑定(bind))

ruby-on-rails - 从 Rails : dependency issues 调用外部 rake 任务

ruby - 如何在 IRB 中重定向 pretty-print

ruby - 将数组的数组转换为数组集

ruby - 在 sinatra 中获取绝对(基本)url

ruby-on-rails - Rails 应用程序的常见用法?