【问题标题】:How can I use optional attributes on an object using Ruby on Rails?如何使用 Ruby on Rails 在对象上使用可选属性?
【发布时间】:2017-07-17 17:13:09
【问题描述】:

我在 Player 模型上有几个属性。我的表单包含要添加的玩家的字段。我想要第二个玩家可以输入他们的名字的可选字段,而不是为第二个玩家重新输入地址和其他内容,我希望第二个玩家继承第一个玩家输入的所有其他信息,而不必再次输入。我怎样才能做到这一点? players/_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 %>

感谢您的任何建议!

【问题讨论】:

  • 想到的 2 个解决方案是,A. 使用 javascript 覆盖提交,如果第二个玩家输入名称,则为两个玩家提交相同的数据。 B. 更新控制器动作以检查第二个玩家名称的存在,并复制该玩家的数据。在我看来,第一个更干净。
  • 谢谢迈克尔,我会尝试这些解决方案。

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


【解决方案1】:

这可能更像是一个javascript 的问题,而不是RoR 的问题。

假设您希望他们可以选择填写不同的信息,我能想到的最简单的方法是为玩家提供一堆隐藏字段信息(除了他们的姓名),给玩家一个字段onkeypress 事件将调用一个函数,该函数将获取相应玩家二字段的 id 并使用该函数查找隐藏的玩家二字段并填写其 html 以与您当前填写的字段相同,类似

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

然后,如果他们开始填写玩家二的姓名,则取消隐藏玩家二的所有字段,以便他们可以选择更改该信息(如果他们选择)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2019-11-29
    相关资源
    最近更新 更多