有两个 model,一个是 user,一个是 education。
class User < ActiveRecord::Base
has_many :educations, dependent: :destroy
accepts_nested_attributes_for :educations
end
class Education < ActiveRecord::Base
belongs_to :user
validates_presence_of :user
end
用户系统使用 devise 建立的,在 users_controller.rb:
def edit
@user = current_user
@educations = @user.educations
@education = Education.new
end
在user#edit页面:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
...
<% end %>
<!-- 上面是用户编辑个人资料的,
我想让用户在这个页面可以添加自己的Education,
所以,直接添加了这个: -->
<%= form_for(@education) do |f| %>
...
<% end %>
报错显示:
undefined method `education' for #<User:0x007fc30e80e6d8>
请问正确的应该怎么做呢?我想用户在自己的编辑页面添加这个资料,
而不是跳转到/users/1/educations/new