after_create url_helpers не работает в пользовательской модели (с Devise) - PullRequest
0 голосов
/ 20 января 2011

Кажется, что url_helpers не работают в моей модели User во время обратного вызова after_create Вот ошибка ...

NameError в Devise / registrationsController # create

неопределенная локальная переменная или метод `controller 'для ...

class User < ActiveRecord::Base

  include ActionView::Helpers::UrlHelper
  include Rails.application.routes.url_helpers

  # Include default devise modules. Others available are:
  # :token_authenticatable, :lockable and :timeoutable
  devise :invitable, :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable
...
# Callbacks
after_create :initialization

  def initialization
if self.temp_org_id.blank?
  self.update_attributes(:email_messages => true, :email_requests => true)
  self.is_travel_planner ? buyer = true : buyer = false


##### THIS IS WHERE IT BREAKS##########################
step_1 = link_to('Setup your personal profile', edit_user_path(self))

  step_2 = link_to('Setup/update your company profile', '/organizations/new?user_id=' + self.id.to_s)
  if buyer == true
    step_3 = link_to "Find potential suppliers by name, location, person, and more!", "/organizations/new?user_id=" + self.id.to_s
  else
    step_3 = link_to "Make Connections with qualified buyers!", "/organizations/new?user_id=" + self.id.to_s
  end
  name = self.name_first
  type = 'suppliers'
  content = "<p>Welcome #{name},</p>
<p>The iTourSmart community is built upon a powerful web-based tool that allows you to find and connect with #{type} like never before.  Building your business with iTourSmart is as easy as 1-2-3!</p>
<h4>Step 1: Setup your personal profile</h4>
<h4>Step 2: #{step_2}</h4>
<h4>Step 3: #{step_3}</h4>
<p>=Of course, you can simply close this window and look around right now.  This message is stored in your message center and can be accessed at any time.</p>"

      new_note = self.notes.create(:is_fancybox_autoload => true, :is_sysmessage => true, :subject => "Setup your brand", :body => content)
    else
      self.roles.create(:user_id => self.id, :organization_id => self.temp_org_id)
      self.update_attributes(:temp_org_id => nil, :email_messages => true, :email_requests => true)
    end
  end
end 

Любая помощь очень ценится!

1 Ответ

2 голосов
/ 20 января 2011

Помощники по URL не работают в моделях AR, не обращая внимания на обратные вызовы.Это сделано специально - ваш уровень модели должен работать вне контекста веб-сервера.

Вы можете решить проблему, включив в модель биты веб-стека, и есть несколько вопросов поТАК, что расскажет вам, как ... Первое в соответствующем списке для этого вопроса:

Можно ли использовать в моделях помощники маршрутизации Rails (т.е. mymodel_path (модель))?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...