0

こんにちは、ネストされたフォームに問題があります..私はそれを1時間見ていて、何を忘れているのかわかりません..

models/trainer.rb

class Trainer < ActiveRecord::Base
  attr_accessible :telephone, :user_attributes
  has_one :user
  accepts_nested_attributes_for :user
end

models/user.rb

class User < ActiveRecord::Base
  belongs_to :trainer

  attr_accessible :email, :image_url, :name, :password_hash, :password_salt, ...
  attr_accessible :password, :password_confirmation
  attr_accessor :password
  before_save :encrypt_password

  <+ validations ...>

controllers/trainers_controller.rb

  def new
    @trainer = Trainer.new
    @trainer.build_user

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @trainer }
    end
  end

新しいトレーナーフォームビューを表示できます(すべてのユーザー列をネストして追加しました)が、CREATEを押すと取得します

Can't mass-assign protected attributes: user

どうしたの ?ありがとうございました

編集:私のdbスキーマは次のようになります

[users]
id
trainer_id
name
surname

[trainers]
telephone

ここで、誰かが興味を持っている場合は、シンプルなアプリをアップロードしました:) https://github.com/ScottHiscock/NestedForm

4

3 に答える 3

0

間違いはビューにありました、私は持っていました

<%= f.fields_for :users do |u| %>

しかし正しいのは

<%= f.fields_for :user do |u| %>

:-)

于 2013-08-14T00:24:42.337 に答える
0

また、次のように、trainer.rb モデルの attr_accessible リストにユーザーを追加します。

attr_accessible :電話、:user_attributes

またはこれを試してください

attr_accessible :電話、:ユーザー

于 2013-08-13T16:31:46.337 に答える