【问题标题】:get user_id Devise in nested form rails 3.1以嵌套形式获取 user_id 设计 rails 3.1
【发布时间】:2012-01-09 16:17:20
【问题描述】:

我将按照本教程http://railscasts.com/episodes/196-nested-model-form-part-1 进行嵌套表单。

我有 3 个模型第一个 user.rb

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

第二个模型是它的board.rb

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

第三个模型它的post.rb

class Post
belongs_to :user
belongs_to :board
end

我想创建一个新帖子,因为我在 boards_controller.rb

中有一个董事会表格
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,例如在控制器的创建操作中,我输入 @post.user = current_user.id 或 @post.user = current_user

如何通过嵌套表单获取嵌套模型帖子中的 user_id?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 nested-forms userid


    【解决方案1】:
    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
    

    【讨论】:

    • 感谢您不为我工作:我正在使用 mongoid。我得到下一个错误:BSON::InvalidDocument (Cannot serialize an object of class User into BSON.): 帖子已创建但 user_id 仍然为零,MONGODB myapp_development['boards'].insert([{"name"=&gt;"board 2", "_id"=&gt;BSON::ObjectId('4f0b6bc51d41c80d08002bcf'), "user_id"=&gt;BSON::ObjectId('4f0b19691d41c80d08002b20'), "slug"=&gt;"board-2", "created_at"=&gt;2012-01-09 22:35:54 UTC}]) Completed 500 Internal Server Error in 5158ms
    • 问题已解决:D。我已经解决了这个 bucle 的问题:for post in @board.posts post.user_id = current_user.id end 非常感谢
    【解决方案2】:

    您应该能够简单地设置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
    

    【讨论】:

    • 谢谢,但是 user_id for Post its nil for me :(. the user_id for board 工作正常,但 user_id for Post 它为零。我不知道如何获取 Post 的 user_id。例如,我需要使用 Post.user.name 或 Post.user.email 访问用户的属性。如果我把 Post.first 我再次得到:&lt;Post _id: 4f0b202e1d41c80d08002b62, _type: nil, created_at: 2012-01-09 17:13:18 UTC, user_id: nil, board_id: BSON::ObjectId('4f0b20291d41c80d08002b61'), content: "Dont working"&gt;
    • 我试过这个:@board.posts.build(:user_id => current_user.id) 但不适合我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多