在我的Rails应用程序中,我在创建新记录后获得了最后一个Expense记录的重复,但我希望在复制时排除一个属性:description_other," "
expenses_controller.rb
class ExpensesController < ApplicationController
# GET /expenses/new
def new
if Expense.last.present?
@expense = Expense.last.dup
else
@expense = Expense.new
end
end
def expense_params
params.require(:expense).permit(:date, :description, :description_other, :trips, :fare, :credit)
end
end
_form.html.erb
<div class="row">
<div class="col-md-3 col-md-offset-4">
<%= form_for(@expense) do |f| %>
<div class="form-group">
<%= f.label :date %><br>
<span class="datetime"><%= f.date_select :date %></span>
</div>
<div class="form-group">
<%= f.label :credit %><br>
<%= f.text_field :credit, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :description %><br>
<%= f.select(:description, options_for_select([['PLEASE SELECT...', ''],['METRO', 'METRO'], ['BUS', 'BUS'], ['TAXI', 'TAXI'], ['OTHERS', 'OTHERS'], ['FOOTLOOSE', 'FOOTLOOSE']]), {}, {class: "form-control", id: "expense_description"}) %>
<br>
<div id="otherDesc">
<%= f.text_field :description_other, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= f.label :trips %><br>
<%= f.number_field :trips, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :fare %><br>
<%= f.text_field :fare, class: "form-control" %>
</div>
<br>
<div style="margin-left: 70px;" class="form-group">
<%= f.submit @expense.new_record? ? "Create Expense" : "Update Expense", class: "btn btn-lg btn-primary" %>
</div>
<% end %>
</div>
</div>
我已经尝试但无法获得所需的结果。
欢迎任何建议。
提前谢谢。
答案 0 :(得分:1)
你应该添加
<div id="otherDesc">
<%= f.text_field :description_other, class: "form-control", value: " "%>
</div>
答案 1 :(得分:1)
使用点按,然后通过设置为nil
排除属性def new
if Expense.last.present?
# exclude here
@expense = Expense.last.dup.tap do | exp |
exp.description_other = nil
end
else
@expense = Expense.new
end
end