Ruby on rails 我的构建方法不再适用于RubyonRails

Ruby on rails 我的构建方法不再适用于RubyonRails,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-3.1,rubygems,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 3.1,Rubygems,在花了几天时间在我的网站的用户配置文件部分工作之后,我现在发现build_配置文件不起作用 我不知道出了什么问题,这没有意义,因为前几天它工作得非常好。现在,它不会在每次创建新用户时都在profiles表中创建一行 以下是我的用户控制器创建操作代码: def create @user = User.new(params[:user]) respond_to do |format| if @user.save @user.build_prof

在花了几天时间在我的网站的用户配置文件部分工作之后,我现在发现build_配置文件不起作用

我不知道出了什么问题,这没有意义,因为前几天它工作得非常好。现在,它不会在每次创建新用户时都在profiles表中创建一行

以下是我的用户控制器创建操作代码:

  def create
    @user = User.new(params[:user])   
    respond_to do |format|
      if @user.save 
        @user.build_profile.save #same as Profile.new(:user_id => @user.id)
        login @user
        UserMailer.join_confirmation(@user).deliver
        format.js   { render :js => "window.location = '#{root_path}'" } 
     #   flash[:notice] = "Welcome!"
      else

        format.js   { render :form_errors }
      end
    end
  end
用户模型:

class User < ActiveRecord::Base

  has_one :profile, :autosave => true
  accepts_nested_attributes_for :profile

  # Setter and getter methods
  attr_accessor :password # virtual password attribute


  # A list of white list of attributes accessible by users in forms
  attr_accessible :email, :username, :password, :password_confirmation
配置文件模型:

class Profile < ActiveRecord::Base

  belongs_to :user



 #  attr_accessor   :password
   attr_accessible :first_name, :last_name, :gender, :motd, :birthday, 
   :marital_status, :sexual_preference, :location, :country, :ethnicity, 
    :about_me, :height, :eye_colour, :body_type, :likes, :dislikes, :drugs,
    :alcohol, :cigarettes, :bad_habits, :food, :music, :television, :book,
    :animal, :place, :possession, :sport
Bang方法错误:

NameError (undefined local variable or method `build_profile' for #<UsersController:0x00000101d63510>):
  app/controllers/users_controller.rb:16:in `block in create'
  app/controllers/users_controller.rb:14:in `create'
这真的很烦人,因为我一直在努力工作,突然它不再工作了

我真的很感激你能帮我 问候

更新

rspec错误:

Failures:

  1) UsersController JOIN 'create' success should create a user
     Failure/Error: post :create, :user => @user
     NoMethodError:
       undefined method `ethnicity' for #<Profile:0x0000010367cab0>
     # ./app/controllers/users_controller.rb:16:in `block in create'
     # ./app/controllers/users_controller.rb:14:in `create'
     # ./spec/controllers/users_controller_spec.rb:96:in `block (5 levels) in <top (required)>'
     # ./spec/controllers/users_controller_spec.rb:95:in `block (4 levels) in <top (required)>'

Finished in 2.75 seconds
47 examples, 1 failure, 1 pending

Failed examples:

rspec ./spec/controllers/users_controller_spec.rb:94 # UsersController JOIN 'create' success should create a user
奇怪!我已经做了检查,并且该属性存在于我的数据库和attr_可访问列表中,所以我很困惑


我删除并重新迁移了,但仍然不走运。

您需要将*profile\u attributes*添加到父模型内的attr\u accessible调用中,在本例中为User。请参见文档:api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

添加:

:before_create :build_profile 

这就是为什么你应该为你的网站编写测试的原因之一。在您实现了一些代码之后,您的测试就会失败,您可以马上看到是什么导致了失败。试着查找内置测试套件,以及Cumber,在我看来这是一个很好的验收测试宝石。我已经安装了rspec,但变得懒惰并停止使用它。我现在确实明白了考试的重要性。第一课。我想因为我建立的不是一个大型站点,所以我可以通过最少的测试来解决问题。我加载了rspec,因为我为我的用户编写了一些测试,它告诉我我有一个未定义的配置文件方法。我已经检查了数据库,它就在那里,我还检查了我的attr_可访问列表@user.build_profile试图使用的build_profile方法在哪里?我看不出来。