没有方法表单中嵌套属性的错误

时间:2013-08-16 17:45:27

标签: ruby-on-rails ruby-on-rails-4

好的,我为nil获取了一个未定义的方法`new':第24行的NilClass,这是在我的devise / registrations / new.html.erb中:

19   <div class="span8">
20     <hr/>
21   </div>
22   <h4 class="span8">Address</h4>
23   <div class="address">
24     <%=f.simple_fields_for :address, collection: [resource.address.new] do |addr_form| %>
25       <div class="span7">
26         <%=addr_form.input :addrl1, required: true, label: "Street Address", input_html: {:class => "input-block-level"}%>
27       </div>
28       <div class="span7">
29         <%=addr_form.input :addrl2, label: "Address Line 2", input_html: {:class => "input-block-level"}%>

设计/注册/ new.html.erb:

<div class="span9 form_user_signup">
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: ""}) do |f| %>
  <%= f.error_notification %>
  <h2 class="form-signin-heading">Sign up</h2>
  <div class="note">
    Please fill out the following appliation to get started. (view <a href="#" target="_blank">privacy policy</a>).
  </div>
  <hr/>
  <h4 class="span8">Contact Information</h4>
  <div class="span7">
   <%= f.input :email, :required => true, :autofocus => true, input_html: {:class => "input-block-level"} %>
  </div>
 <div class="span7">
   <%= f.input :password, :required => true, input_html: {:class => "input-block-level"}  %>
 </div>
 <div class="span7">
   <%= f.input :password_confirmation, :required => true, input_html: {:class => "input-block-level"} %>
 </div>
 <div class="span8">
   <hr/>
 </div>
 <h4 class="span8">Address</h4>
 <div class="address">
   <%=f.simple_fields_for :address, collection: [resource.address.new] do |addr_form| %>
     <div class="span7">
       <%=addr_form.input :addrl1, required: true, label: "Street Address", input_html: {:class => "input-block-level"}%>
     </div>
     <div class="span7">
       <%=addr_form.input :addrl2, label: "Address Line 2", input_html: {:class => "input-block-level"}%>
     </div>
     <div class="span4">
       <%=addr_form.input :city, required: true, label: "City"%>
     </div>
     <div class="span4">
       <%=addr_form.input :state, required: true, collection: states, label: "State", value_method: :abbr, include_blank: 'Select State'%>
     </div>
     <div class="span4">
       <%=addr_form.input :region_code, required: true, label: "Postal / Zip Code"%>
     </div>
   <% end %>
 </div>
 <div class="span8">
   <hr/>
 </div>
 <div class="form-inputs">
   <%= f.button :submit, "Sign up", class: "btn btn-large btn-primary " %>
 </div>
 <div class="form-inputs pull-right">
   <%= link_to "Already have an account?", new_user_session_path, class: " " %>
 </div>
<% end %>
</div>

user.rb:

class User < ActiveRecord::Base
   devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, 
         :confirmable, :lockable

  has_one :address
end

address.rb:

class Address < ActiveRecord::Base
  attr_accessible :name, :addrl1, :addrl2, :city, :state, :region_code

  belongs_to :user
end

感谢您的帮助 如果有更多信息你需要帮助我将提供它。

1 个答案:

答案 0 :(得分:1)

您的资源(用户)此时没有地址,因此您想要build一个:

 <%=f.simple_fields_for :address, collection: [resource.build_address] do |addr_form| %>