jwilcox09
jwilcox09

Reputation: 721

Rails url helpers not available in name spaced controller

I have a name spaced controller that is from a gem I am creating, and it seems that none of the *_path or *_url helpers that come from the routes are available for any of the other models when trying to load one of the actions. I can't seem to figure out why they are not available in just this 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

Routes

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

Every other route is fine but these. I get the error:

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

Team is another model and I have a partial that calls this, this just happens to be the first call to *_path, if I change this it just keeps failing further down the render. Here is the html:

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

current_user is indeed defined and available

Any ideas? I've given a good search but everything is related to people not defining @variable in their controller method for a page with a form, which is not the case for me.

EDIT (SOLUTION):

Turns out for some reason when I add helper Rails.application.routes.url_helpers into my controller everything works fine. This seems to be more a bug in rails than anything, as i shouldn't have to do that, but oh well.

Upvotes: 2

Views: 1501

Answers (1)

A Fader Darkly
A Fader Darkly

Reputation: 3626

Try using:

app.team_path

to access the path helper where you wouldn't normally (like in the console).

It is puzzling that just namespacing the controller would do this though. I'll try to recreate at some point.

Upvotes: 1

Related Questions