我已經堆棧現在充溢了一段時間,我已經做了一些教程/網路上的演練,包括RailsGuides & Railscasts,但我似乎遇到了問題救了我的嵌套的屬性我數據庫。嵌套屬性不保存到ActiveRecord的
我的投注,其中有許多條款
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
我的投注控制器
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糖風格),驗證等等。 : -/
Running Rails 3.2.13。
謝謝艾拉!主機條款現在保存。我現在正在努力讓客人也這樣做。 –