我在显示嵌套控制器中的值时遇到问题。我可以很好地更新字段,只是在显示页面上看不到值。我认为这是Devise的问题。
我的用户模型如下:
class User < ActiveRecord::Base
has_one :page
accepts_nested_attributes_for :page, :allow_destroy => true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name
has_friendly_id :name, :use_slug => true, :strip_non_ascii => true
validates_presence_of :name
validates_uniqueness_of :name
validates_uniqueness_of :email, :case_sensitive => false
#creates a new page on user create if one doesn't exist
def after_initialize
self.build_page if self.page.nil?
end
end
我的页面模型:
class Page < ActiveRecord::Base
belongs_to :user
attr_accessible :tagline, :about, :email, :phone, :website, :blog, :user,
end
路线.rb
Ahoy::Application.routes.draw do
resources :users, :path => '/' do
resource :page
end
devise_for :users, :controllers => { :registrations => "registrations"}
get "home/index"
get "home/about"
root :to => "home#index"
end
在我的用户#show中,我有这个:
<p>
<strong>Tagline</strong>
<%= @user.tagline %>
</p>
我得到了一个未定义的方法。也尝试过,@pages.标语等。
我认为我不需要修改我的控制器?你能帮忙吗?
tagline
方法在 Page
中定义,所以应该@user.page.tagline