2012-01-09 4 views
0

私はこのチュートリアルhttp://railscasts.com/episodes/196-nested-model-form-part-1のネストフォームに従っています。get user_id入れ子フォームレールの開発3.1

class User 
has_many :boards, dependent: :destroy 
has_many :posts, dependent: :destroy, :autosave => true 
accepts_nested_attributes_for :boards 
accepts_nested_attributes_for :posts 

end 

第2モデルにそのboard.rb

class Board 
has_many :posts, :dependent => :destroy , :autosave => true 
accepts_nested_attributes_for :posts 
belongs_to :user 
end 

第3のモデルそのpost.rb

Iは3モデル最初user.rbを有します

class Post 
belongs_to :user 
belongs_to :board 
end 

私はボードのフォームから新しい投稿を作成したいと私は私の意見に投稿のすべての属性を取得boards_controller.rbこの2つの方法で

def new 
    @board = Board.new 
    @board.posts.build 
respond_to do |format| 
    format.html # new.html.erb 
    format.json { render json: @board } 
end 
end 

def create 
@board = current_user.boards.new(params[:board]) 
@board.user = current_user 
respond_to do |format| 
    if @board.save 
    format.html { redirect_to @board, notice: 'Board was successfully created.' } 
    format.json { render json: @board, status: :created, location: @board } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @board.errors, status: :unprocessable_entity } 
    end 
end 
end 

に私が持っています。私は後にPost.firstボードを作成置けば私のコンソールに私が取得:

1.9.2-p290 :007 > Post.first 
=> #<Post _id: 4f0b0b211d41c80d08002afe, _type: nil, created_at: 2012-01-09 15:43:29 UTC, user_id: nil, board_id: BSON::ObjectId('4f0b0b1b1d41c80d08002afd'), content: "hello post 2"> 

しかし、あなたは私がのuser_idを得る見て取る場合: nilを。私は、例えば、ユーザIDを取得し、通常のモデルでは

は、コントローラのアクションを作成し、私は、ネストされた中でのuser_idを取得できますかpost.user = current_user.idまたは@ post.user = CURRENT_USER

@を置きますモデルはネストされたフォームから投稿されますか?

答えて

2
def create 
@board = current_user.boards.new(params[:board]) 
#@board.user = current_user - you don't need this line, since you create new board record using current_user object 
# assign current_user id to the each post.user_id 
@board.posts.each {|post| post.user_id = current_user} 

respond_to do |format| 
    if @board.save 
    format.html { redirect_to @board, notice: 'Board was successfully created.' } 
    format.json { render json: @board, status: :created, location: @board } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @board.errors, status: :unprocessable_entity } 
    end 
end 
end 
+0

私のために働いてくれてありがとう:私はモンゴイドを使用しています。 'BSON :: InvalidDocument(BSONにUserクラスのオブジェクトを直列化できません):'投稿は作成されていますが、user_idはまだありません。 'MONGODB myapp_development ['boards']。insert([{" BSON :: ObjectId( '4f0b6bc51d41c80d08002bcf')、 "user_id" => BSON :: ObjectId( '4f0b19691d41c80d08002b20')、 "slug" => "board-2"、 "created_at" => 2012-01-09 22:35:54 UTC}]) 5158msの内部サーバーエラー5003を完了しました。 ' – hyperrjas

+0

問題が修正されました:D。私はこのブールで問題を解決しました: 'post for @ board.posts post.user_id = current_user。id end'ありがとうございました – hyperrjas

0

単純にuser_idプロパティを設定することができます。

コードでは、current_userオブジェクトを関連付けに割り当てています。

これは動作するはずです:

def create 
@board = current_user.boards.new(params[:board]) 
@board.user_id = current_user.id 
respond_to do |format| 
    if @board.save 
    format.html { redirect_to @board, notice: 'Board was successfully created.' } 
    format.json { render json: @board, status: :created, location: @board } 
    else 
    format.html { render action: "new" } 
    format.json { render json: @board.errors, status: :unprocessable_entity } 
    end 
end 
end 
+0

はあなたに感謝しますが、私にとってのuser_idは**ポストのためのゼロ** :(ボード用のuser_idが正常に動作しますが、ポストのためにそのnilをUSER_ID。 Post.user.nameやPost.user.emailを使ったPostからUserの属性にアクセスする必要があります。Post.firstを置くと、次のようになります。 ' ' – hyperrjas

+0

私はこれを試しました:@ board.posts.build(:user_id => current_user.id)bu私のために働かないでください。 – hyperrjas

関連する問題