我在嵌套控制器中显示值时遇到问题。我可以很好地更新字段,我只是看不到显示页面上的值。我认为这是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
的routes.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等。
我认为我不需要修改我的控制器?你能帮忙吗?
答案 0 :(得分:1)
tagline
方法在Page
中定义,因此它应为@user.page.tagline