0

私のウェブサイトのユーザープロファイルセクションで働いていた日の後に、build_profileが動作しないことが判明しました。私のビルドメソッドは、もはやRuby on Railsで動作していません

私は何が間違っているのか分かりませんが、先日完全にうまく動作していたので意味がありません。今度は、新しいユーザーが作成されるたびに、プロファイルテーブルに行が作成されません。

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 

Profileモデル:

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 

バン方法エラー:

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' 
0123をここで

は私のユーザーのコントローラはアクションコードを作成することです

私はこのことに熱心に取り組んでいて、もはやうまくいきません。

この 種類が

をに関しては、私はいくつかの助けを本当に感謝アップデート

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 

奇妙な!チェックを済ませて、その属性がdbとattr_accessibleリストに存在するので、私は非常に困惑しています。

私は落として再移行してもまだ運がありません。

+0

これは、あなたのウェブサイトのテストを書くべき理由の1つです。いくつかのコードを実装した後、テストは失敗し、すぐに何があったのか分かりました。私の意見では、受入れテストの宝石であるキュウリだけでなく、組み込みのテストスイートを調べてみてください。 – MrDanA

+0

私はrspecをインストールしましたが、怠け者になって使用を中止しました。私は本当に今テストの重要性を理解しています。最初に学んだレッスン。それは私が構築していた大規模なサイトではないので、私は最小限のテストで逃げることができると思った。 – LondonGuy

+0

私はusers_controllerのために書かれたテストがいくつかあったので、私はrspecをロードしました。プロフィールのための未知のメソッド "ethnicity"を持っていると言います。私はDBをチェックしてそこにあり、私はattr_accessibleリストもチェックしました – LondonGuy

答えて

0

追加:

:before_create :build_profile 

をユーザモデルには、トリックを行いました。

0

親モデル内のattr_accessible呼び出し(この場合はUser)に* profile_attributes *を追加する必要があります。ドキュメントを参照してください。api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

関連する問題