1
我想构建一个简单的应用程序,其中用户可以发布内容并将问题附加到帖子(post.questions)。rails 4 f.fields_for不会遍历我的嵌入式集合
我收到embeds_many方案中显示问题的问题。虽然我在PostsController#new方法中创建了多个问题,但是渲染后的html只显示一个问题。
我正在使用rails4/mongoid/mongodb。
Post.rb:
class Post
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :body, type: String
embeds_many :post_replies
embeds_many :questions
belongs_to :poster, class_name: "User"
accepts_nested_attributes_for :questions
end
Quesion.rb。
class Question
include Mongoid::Document
include Mongoid::Timestamps
field :q
embedded_in :post
end
PostsController.rb
class PostsController < ApplicationController
def new
@post = Post.new
3.times{
post.questions.build
}
end
def post_params
params.require(:post).permit(:title, :body, questions_attributes: [:id, :q, :_destroy])
end
end #end controller
的routes.rb devise_for:用户 资源:用户做 资源:帖子 资源:地址 结束 的意见/职位/ _form.html.erb
<%= form_for([current_user, @post], :html => { :class => "form-horizontal" }) do |f| %>
-
-
<%=f.fields_for @post.questions do|question| %>
<%= question.text_field :q, :placeholder =>'question', :class => "form-control" %>
<%end%>
-
-
<%end%>
我期望以上字段为d isplay 3个问题,但它只显示一个问题。我尝试了fields_for的不同方式,但都没有成功。
在此非常感谢任何帮助。
在此先感谢。