我已经有一段时间Stack Overflowing了,我已经在网上完成了一些教程/演练,包括RailsGuides& Railscasts,但我似乎无法将嵌套属性保存到我的数据库。
我有Wagers,有很多条款
class Wager < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :host, class_name: "User", foreign_key: "host_id"
belongs_to :guest, class_name: "User", foreign_key: "guest_id"
has_many :terms, :dependent => :destroy
accepts_nested_attributes_for :terms, allow_destroy: true , :reject_if => lambda { |a| a[:terms].blank? }
attr_accessible :title, :description, :terms, :terms_attributes
end
和条款
class Term < ActiveRecord::Base
belongs_to :wager
attr_accessible :title, :body, :criterion, :host_criterion, :guest_criterion, :terms_attributes
end
我的Wagers控制器
class WagersController < ApplicationController
before_filter :authenticate_user!
def index
end
def create
# Create new wager from params
@wager = Wager.new(params[:wager])
# Assign current user
@wager.host_id = current_user.id
# Assign guest user here
# ______________________
@term = @wager.terms.build(params[:terms])
if @wager.valid?
respond_to do |format|
if @wager.save
format.html { redirect_to(@wager, :notice => 'Wager Stub successfully created.') }
format.xml { render :xml => @wager, :status => :created, :location => @wager }
else
format.html { render :action => "new" }
format.xml { render :xml => @wager.errors, :status => :unprocessable_entity }
end
end
end
def new
@wager = Wager.new
@term = Term.new
3.times { @wager.terms.build }
end
end
我的新观点:
<h2>Create a Wager</h2>
<%= form_for @wager, url: {action: "create"}, html: {class: ""} do |f| %>
<fieldset>
<legend>Create a wager</legend>
<div class="row">
<div class="large-12 columns">
<label>Title</label>
<%= f.text_field :title, placeholder: "The Bet To End All Bets" %>
</div>
</div>
<div class="row">
<div class="large-4 columns">
<label>With</label>
<%#= f.text_field :guest, placeholder: "Choose a Friend" %>
</div>
<div class="large-8 columns">
</div>
</div>
<div class="row">
<div class="large-12 columns">
<label>Description</label>
<%= f.text_area :description, size: "60x12", placeholder: "What's all this about?" %>
</div>
</div>
<div class="row">
<div class="large-5 columns">
<div class="row collapse">
<h4>Your Terms</h4>
</div>
<%= f.fields_for :terms do |term_form| %>
<%= term_form.label :host_criterion, 'Term:' %>
<%= term_form.text_field :host_criterion %>
<% end %>
</div>
<div class="large-5 columns">
<div class="row collapse">
<h4>Their Terms</h4>
</div>
<%= f.fields_for :terms do |term_form| %>
<%= term_form.label :guest_criterion, 'Term:' %>
<%= term_form.text_field :guest_criterion %>
<% end %>
</div>
</div>
<div class="row">
<div class="large-8 columns">
</div>
<div class="large-4 columns">
<%= f.submit "Create Stub", class: "button" %>
</div>
</div>
</fieldset>
我的参数看起来像这样:( SPOILER ALERT:它们都是零。除了它确实分配了一个赌注ID。很确定这是由于构建方法)
=>
"wager"=>
{"title"=>"Title of the Bet",
"description"=>"Bet description",
"terms_attributes"=>
{"0"=>{"host_criterion"=>"You gotta be nude"},
"1"=>{"host_criterion"=>"no drinking"},
"2"=>{"host_criterion"=>"can't brush your hair"},
"3"=>{"guest_criterion"=>"Guest Term 1"},
"4"=>{"guest_criterion"=>"Guest Term 2"},
"5"=>{"guest_criterion"=>"Guest Term 3"}}},
"commit"=>"Create Stub",
"action"=>"create",
"controller"=>"wagers"}
然而,当保存记录时,我的服务器日志显示:
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 5 LIMIT 1
(0.6ms) begin transaction
SQL (1.9ms) INSERT INTO "wagers" ("created_at", "deadline", "description", "guest_id", "host_id", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?) [["created_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00], ["deadline", nil], ["description", "Bet description"], ["guest_id", nil], ["host_id", 5], ["title", "Title of the Bet"], ["updated_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00]]
SQL (0.3ms) INSERT INTO "terms" ("created_at", "criterion", "guest_criterion", "host_criterion", "updated_at", "wager_id") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00], ["criterion", nil], ["guest_criterion", nil], ["host_criterion", nil], ["updated_at", Thu, 17 Oct 2013 23:50:50 UTC +00:00], ["wager_id", 48]]
(1.0ms) commit transaction
Redirected to http://localhost:3000/wagers/48
Completed 302 Found in 121668ms (ActiveRecord: 5.9ms)
我错过了什么?目标是为与赌注一起提交的每个术语(最多6个)创建一个术语。我很感激任何帮助,因为我在网上找到的演练在很大程度上引导我到这一点,但它最终为他们工作(Rails糖风格),验证和所有。 : - /
运行Rails 3.2.13。
答案 0 :(得分:0)
我会将@term用于them_terms。尝试一下,让我知道它是否有效。
或者更好的是使用术语表为他们的_terms创建一个新的关系。
答案 1 :(得分:0)
, :reject_if => lambda { |a| a[:terms].blank? }
正在停止工作。
相反,它应该是:reject_if: lambda { |terms| terms[:host_criterion].blank? }
注意:为了清晰起见,我将“a”替换为“条款”
第一个问题是rails将在“terms”表中查找名为“terms”的字段。相反,它应该寻找“host_criterion”。
revised railscast完全删除了此方法,并使用coffeescript验证在客户端执行此操作(防止条件表中的空记录)。