ユーザーとプロファイルの2つのモデルがあります(ユーザーはdeviseからのものです)。ユーザーは多くのプロファイルを持つことができるため、プロファイルに user_id を追加するための移行を作成しました。
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessible :description, :name, :product, :image, :price , :user_id
mount_uploader :image, ImageUploader
validates_presence_of :name, :product,:image, :price
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :profiles
validates_associated :profiles
end
私のプロファイルコントローラーには、新しいプロファイルを作成する新しいメソッドがあります。ユーザーがログインしたときに、すべてではなく自分のプロファイルのみを表示できるようにしたい。新しい定義
@profile = current_user.profiles.build
#@profile = @user.profile.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @profile }
end
終わり
理想的には、ビルドで user_id 列を更新する必要がありますが、user_id が更新されないため、プロファイルが表示されません。私はpg gemでpostgresqlを使用しています。そのプロファイルのuser_idを手動で追加すると、プロファイルを見ることができます。
これを解決するのを手伝ってください。私は長い間これを理解することができます.これを解決するための詳細情報が必要な場合はお知らせください.