如何使用Ruby on Rails在对象上使用可选属性?

时间:2017-07-17 17:13:10

标签: ruby-on-rails ruby forms twitter-bootstrap

我在Player模型上有几个属性。我的表单包含要添加的播放器的字段。我希望有一个可选字段,其中第二个玩家可以输入他们的名字,而不是必须为第二个玩家重新输入地址和其他东西,我希望第二个玩家继承第一个玩家输入的所有其他信息而不必再次输入。我怎样才能做到这一点? 的播放器/ _form.html.erb

<%= form_for @player, html: {class: "horizontal-form"} do |player| %>

  <div class="form-group">
    <div class = row>
      <%= player.label :first_name, "First Name", class: "col-sm-2 control-label"%>
      <div class="col-sm-offset-2">
        <%= player.text_field :first_name, class: "form-control", :required => true %>
      </div>
    </div>  
  </div>

  <div class="form-group">
    <div class = row>
      <%= player.label :last_name, "Last Name", class: "col-sm-2 control-label"%>
      <div class="col-sm-offset-2">
        <%= player.text_field :last_name, class: "form-control", :required => true %>
      </div>
    </div>
  </div>

  <div class="form-group">
    <div class = row>
      <%= player.label :address, "Address", class: "col-sm-2 control-label"%>
      <div class="col-sm-offset-2">
        <%= player.text_field :address, class: "form-control", :required => true %>
      </div>
    </div>  
  </div>

  <div class="form-group">
    <div class = row>
      <%= player.label :city, "City", class: "col-sm-2 control-label"%>
      <div class="col-sm-offset-2">
        <%= player.text_field :city, class: "form-control", :required => true %>
      </div>
    </div>  
  </div>

  <div class="form-group">
    <div class = row>
      <%= player.label :state, "State", class: "col-sm-2 control-label"%> 
      <div class="col-sm-offset-2">
        <%= player.text_field :state, class: "form-control", :required => true %>
      </div>
    </div>  
  </div>

  <div class="form-group">
    <div class = row>
      <%= player.label :zip_code, "Zip Code", class: "col-sm-2 control-label"%>
      <div class="col-sm-offset-2">
        <%= player.text_field :zip_code, class: "form-control", :required => true %>
      </div>
    </div>  
  </div>

  <div class="form-group">
    <div class = row>
      <%= player.label :email, "Email", class: "col-sm-2 control-label"%>
      <div class="col-sm-offset-2">
        <%= player.text_field :email, class: "form-control", :required => true %>
      </div>
    </div>  
  </div>

  <div class="form-group">
    <div class = row>
      <%= player.label :veteran, "Veteran", class: "col-sm-2 control-label"%>
      <div class="checkbox col-sm-offset-3">
        <%= player.check_box :veteran %>
      </div>
    </div>  
  </div>  

  <div class="form-group">
    <div class = row>
    <%= player.label :branch_id, "Branch", class: "col-sm-2 control-label" %>
    <%= player.collection_select(:branch_id, Branch.all, :id, :name, :prompt => true) %>
    </div>  
  </div>

  <%= player.submit 'Submit', class: 'col-sm-2 col-sm-offset-2 btn btn-primary' %>

<% end %>

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

这可能更多是javascript问题,而不是RoR问题。

假设你希望他们可以选择填写不同的信息,我能想到的最简单的方法就是为玩家提供一堆隐藏的字段两个信息(除了他们的名字),给玩家一个字段onkeypress事件将调用一个函数,该函数将接收相应的播放器两个字段的id,并使用它来查找隐藏的播放器两个字段并填写他们的html与您所在的字段相同目前正在填写,如

function fillPlayerTwoField(id) {
  $(id).text(this.value)
}

然后如果他们开始填充玩家,则两个名称取消隐藏所有玩家两个字段,以便他们可以选择更改该信息