0

関連を定義する User モデルがありますhas_one :profile

ユーザー編集アクション/ページで、ユーザー属性を更新します。UserモデルではなくProfileモデルに属している属性を更新したいと考えています。

これは私の見解です:

# _form.html.haml
=f.text_field :phone

このフォームを使用して、どうすれば のphone属性も更新できます@user.profileか?

4

2 に答える 2

1

ユーザーモデルのネストされた属性の更新を許可する必要があります。

class User
    has_one :profile
    accepts_nested_attributes_for :profile

end

次に、フォームでこのfields_forメソッドを使用して、プロファイルのフィールドをユーザーフォームにネストします。

= f.fields_for :profile do |p|
  = p.text_field :phone
于 2013-01-10T15:43:52.297 に答える
0

2 つのテーブル user と profile が関連付けられている場合の一般的な考え方として、次のようになります。

ユーザー テーブル = id、first_name、last_name

プロファイル テーブル = id、user_id、電話

協会:

User Class-

class User < ActiveRecord::Base
  has_one :profile
end

Profile Class-

class Profile < ActiveRecord::Base
  belongs_to :user
end

User Controller-

def index
  @user = current_user
  @user_phone = @user.profile.phone

end
于 2013-01-10T15:35:04.440 に答える