多记录提交,无需嵌套
我确信我已经过度思考这个问题了,但我似乎无法弄清楚如何简单地一次创建和提交多个记录。我有一个用户模型和一个预测模型。用户有很多预测,并且预测属于一个用户。我已经像这样嵌套了我的路线,
:resources users do
:resources predictions
end
当我访问 users/1/predictions/new 时,我需要创建 6 个预测记录,并将它们立即提交到数据库。
在我的预测控制器中:
before_filter :load_user
def new
3.times { @user.predictions.build }
end
def create
@prediction = @user.predictions.new(params[:prediction])
if @prediction.save
redirect_to @user, :notice => 'Prediction added'
else
redirect_to @user, :notice => 'Unable to add'
end
end
def destroy
@prediction = @user.prediction.find(params[:id])
@prediction.destroy
redirect_to @user, :notice => "Prediction deleted"
end
private
def load_user
@user = current_user
end
在我的预测 new.html.erb 中:
<%= form_for ([@user, @user.predictions.new]) do |f| %>
<div class="fields">
<%= f.label :position %>
<%= f.text_field :position %>
</div>
<div class="fields">
<%= f.label :athlete_id, 'Athlete'%>
<%= f.collection_select(:athlete_id, Athlete.all, :id, :name, :prompt => 'Select an athlete' )%>
</div>
<div class="fields">
<%= f.label :race_id, 'Race'%>
<%= f.collection_select(:race_id, Race.upcoming, :id, :name, :prompt => 'Select a race' )%>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
这仅显示并提交一条记录而不是 3 条。我想我可能必须使用 :accepts_nested_attributes_for,但是我不需要在同一时间。现有用户将为多场比赛一次创建 3 条记录的预测,因为这是一款梦幻体育应用程序。
I'm sure I'm over thinking this problem, but I can't seem figure out how to simply create and submit multiple records at once. I have a User model and a Prediction model. User has_many predictions, and a Prediction belongs_to a user. I've nested my routes like so
:resources users do
:resources predictions
end
when I visit users/1/predictions/new, I need to create 6 Prediction records, and submit them at once to the db.
In my Predictions controller:
before_filter :load_user
def new
3.times { @user.predictions.build }
end
def create
@prediction = @user.predictions.new(params[:prediction])
if @prediction.save
redirect_to @user, :notice => 'Prediction added'
else
redirect_to @user, :notice => 'Unable to add'
end
end
def destroy
@prediction = @user.prediction.find(params[:id])
@prediction.destroy
redirect_to @user, :notice => "Prediction deleted"
end
private
def load_user
@user = current_user
end
And in my Prediction new.html.erb:
<%= form_for ([@user, @user.predictions.new]) do |f| %>
<div class="fields">
<%= f.label :position %>
<%= f.text_field :position %>
</div>
<div class="fields">
<%= f.label :athlete_id, 'Athlete'%>
<%= f.collection_select(:athlete_id, Athlete.all, :id, :name, :prompt => 'Select an athlete' )%>
</div>
<div class="fields">
<%= f.label :race_id, 'Race'%>
<%= f.collection_select(:race_id, Race.upcoming, :id, :name, :prompt => 'Select a race' )%>
</div>
<div class="actions"><%= f.submit %></div>
<% end %>
This shows and submits only one record instead of 3. I thought I might have to use :accepts_nested_attributes_for, however I don't need to create and update User models at the same time. Existing Users will be creating predictions 3 records at a time for several Races, as this is a fantasy sports app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为第一项,嵌套路由可能不是您正在寻找的方法。这可能会将您绑定到表单上的 1 个新模型预测记录。
您确实希望在模型中使用
accepts_nested_attributes_for
。对于该设置,请使用 form_for (如果可能的话,使用 simple_form_for,使用 simple_form gem)。然后用类似的代码form_for @user 做 |f|
使用 f.fields_for :prediction
用户控制器保存方法还将自动验证并保存嵌套记录。
您可能知道 Ryan Bates 为此提供了出色的 railscasts。
I think that the very first item, the nested route may not be the approach you are looking for. That may tie you down to 1 new model prediction record on the form.
You do indeed want
accepts_nested_attributes_for
in your model. With that set use a form_for (and simple_form_for if possible, using the simple_form gem). Then with code likeform_for @user do |f|
use f.fields_for :prediction
The user controller save methods will also validate and save the nested records automatically.
As you may know Ryan Bates has great railscasts for this.