2011-01-20 35 views
0

在after_create回調期間,url_helpers似乎不能在我的用戶模型中工作。以下是錯誤...after_create url_helpers不能在用戶模型中工作(與設計)

NameError在設計/ registrationsController#創建

未定義的局部變量或方法'控制」了......

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 

任何幫助,不勝感激!

回答

2

Url助手不能在AR模型中工作,完全停止,不需要回調。這是通過設計 - 您的模型層應該能夠在Web服務器的上下文之外運行。

您可以通過在該模型中的網絡堆棧的位捏造的問題,並有上,這樣會告訴你幾個問題在相關列表中的第一個爲這個問題怎麼..是:

Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models?

+0

謝謝。經過更多的研究後,我認爲這個問題也源於Devise的使用。我已經爲我的問題添加了更多信息。 – 2011-01-20 20:27:41

相關問題