我正在尝试构建一个小费用跟踪应用。使用nested_form gem添加订单项。有一个Expense模型接受嵌套属性。项目属于费用,并且有外键关联。这是费用控制器:
class ExpensesController < ApplicationController
def new
@expense = Expense.new
@item = @expense.items.build
end
def index
@expenses = Expense.all
#@items = Item.where(:expense_id => @expense.id)
end
def show
@expense = Expense.find(params[:id])
@items = Item.where(:expense_id => @expense.id)
end
def create
@expense = Expense.new(expense_params)
respond_to do |format|
if @expense.save
format.html { redirect_to @expense, notice: 'Expense Report Submitted.' }
format.json { render :show, status: :created, location: @expense }
else
format.html { render :new }
format.json { render json: @expense.errors, status: :unprocessable_entity }
end
end
end
def edit
@expense = Expense.find(params[:id])
end
def update
@expense = Expense.find(params[:id])
if @expense.save(expense_params)
flash[:notice] = "Expense Report Updated"
redirect_to @expense
else
render 'edit'
end
end
def destroy
@expense = Expense.find(params[:id])
@expense.destroy
redirect_to 'root'
end
private
def expense_params
params.require(:expense).permit(:department_id, :expense_type_id, :notes, items_attributes: [:id, :description, :amount, :issue_date, :_destroy])
end
end
表格如下:
<%= nested_form_for (@expense) do |f| %>
<% if @expense.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@expense.errors.count, "error") %> prohibited
this expense from being saved:</h2>
<ul>
<% @expense.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class"row">
<div class="col-md-8">
<div class="form-group">
<%= f.label :department_id %><br>
<%= f.collection_select(:department_id, Department.all, :id, :department_name, prompt: true, class: "dropdown-menu") %>
</div>
<div class="form-group">
<%= f.label :expense_type_id %><br>
<%= f.collection_select(:expense_type_id, ExpenseType.all, :id, :expense_name, prompt: true, class: "form-control") %>
</div>
<%= f.fields_for :items do |i| %>
<div class="form-group">
<%= i.label :description%>
<%= i.text_field :description, class: "form-control" %>
</div>
<div class="form-group">
<%= i.label :amount%>
<%= i.text_field :amount, class: "form-control" %>
</div>
<div class="form-group">
<%= i.label :issue_date%>
<%= i.date_select :issue_date, class: "form-control" %>
</div>
<%= i.link_to_remove "Remove", class: "btn btn-default" %>
<% end %>
<div><p><%= f.link_to_add "Add Expense", :items, class: "btn btn-default" %></p></div>
<div class="form-group">
<%= f.label :notes %>
<%= f.text_area :notes, class: "form-control" %>
</div>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
当我点击提交时,费用和项目都会保存。我检查了Sqlite浏览器,并为每个项目捕获了外键值。
索引文件如下所示:
<% @expenses.each do |expense| %>
<tr>
<td><%= expense.item.description %></td>
<td><%= number_to_currency(expense.item.amount) %></td>
<td><%= expense.item.issue_date %></td>
<% end %>
我通过一个块尝试了通常的组合(例如expense.item.description),但它们无法正常工作。我想知道如何在show和index页面中显示费用和相关项目。
答案 0 :(得分:1)
看看你的新动作,在你的模特中你会有
class Expense < ActiveRecord::Base
has_many :items
accepts_nested_attributes_for :items
end
因此,当您在视图中执行expense.items时,它会为您提供有效的记录关系(因为费用包含很多项目),请尝试以下操作:
<% @expenses.each do |expense| %>
<% expense.items.each do |item| %>
<tr>
<td><%= item.description %></td>
<td><%= number_to_currency(item.amount) %></td>
<td><%= item.issue_date %></td>
</tr>
<% end %>
<% end %>