我有一个 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/