【发布时间】:2015-02-26 14:35:46
【问题描述】:
我正在尝试使用为 Ruby/Rails 项目创建的标准创建方法,并简单地传入一个额外的表单字段,该字段告诉该方法要创建多少个对象(而不是只创建一个对象)。标准的 create 方法如下所示:
def create
@micropost = Micropost.new(micropost_params)
respond_to do |format|
if @micropost.save
format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
format.json { render :show, status: :created, location: @micropost }
else
format.html { render :new }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end
我想传入一个额外的数据(名为 number_to_create 的表单字段),它告诉方法要创建多少微博。我刚刚添加了一个这样的新表单字段,除了其他微帖子表单字段参数:
<%= text_field_tag :number_to_create %>
我的问题是如何修改 create 方法代码,使其创建 N 个 micropost 对象而不是一个。因此,如果我从表单中传入 3 以及其他 micropost 属性,则该方法会创建 3 个相同的 micropost 对象,而不是像现在这样。
在此先感谢您的帮助。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4