0

我在 Stack Overflow 上已经有一段时间了,并且我已经在网络上完成了一些教程/演练,包括 RailsGuides 和 Railscasts,但我似乎无法将嵌套属性保存到我的数据库中。

我有投注,其中有很多条款

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>

我的参数看起来像这样:(剧透警报:它们都是零。除了它确实分配了一个赌注 ID。很确定这是由于 build 方法)

=> 
"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。

4

2 回答 2

0

, :reject_if => lambda { |a| a[:terms].blank? }正在阻止它工作。

相反,它应该是:reject_if: lambda { |terms| terms[:host_criterion].blank? } 注意:为了清楚起见,我将“a”替换为“terms”

第一个问题是 rails 会在“terms”表中查找名为“terms”的字段。相反,它应该寻找“host_criterion”。

修改后的railscast完全删除了这个方法,并使用 coffeescript 在客户端验证这样做(防止条款表中的空记录)。

于 2013-10-27T18:53:28.550 回答
0

我会将@term 用于 they_terms 的事情。试试看,让我知道它是否有效。

或者更好的是使用术语表为 their_terms 创建一个新的关系。

于 2013-10-18T01:11:08.593 回答