Ruby on Rails:一对多关系视图

时间:2021-07-02 09:57:40

New to rails and trying to get a one to many relationship up and running. I have it working in the Model and Controller, but I'm having trouble getting it up and running in the views.

对rails很新,并尝试建立一对多的关系并运行。我让它在模型和控制器中工作,但我无法在视图中运行并运行。

class Project < ActiveRecord::Base
  has_many :non_labor_expenses
end

class NonLaborExpense < ActiveRecord::Base
  belongs_to :project
end

I manually created some entries in the the non_labor_expense table by loading the @non_labor_expenses in the controller (edit action) and can pull the existing data in the project view like so:

我通过在控制器中加载@non_labor_expenses(编辑操作)手动在non_labor_expense表中创建了一些条目,并且可以在项目视图中提取现有数据,如下所示:

<% unless @non_labor_expenses.nil? %>
  <% count = 1 %>
  <% for expense in @non_labor_expenses %>
    <li>
      <div class="label"><%= f.label :expense , "Expense" + count.to_s %></div>
      <%= '$' + expense.amount.to_s + ' - ' + expense.description.to_s %>

    </li>
  <% count = count +1 %>
  <% end %>

What I am having trouble doing is adding a new non_labor_expense entry to the project. I should be able to manage handling it on the backend, but I can't even get the field to show up in the form.

我无法做的是在项目中添加一个新的non_labor_expense条目。我应该能够管理后端处理它,但我甚至无法让字段显示在表单中。

Here's where I'm at now:

这就是我现在所处的位置:

<li class="editable">
  <div class="label"><%= f.label :non_labor_expenses %></div>
  <%= f.text_field :non_labor_expenses %>
</li>

I know my above code looks nothing like this, but ideally the form fields would be something like:

我知道我的上面的代码看起来不像这样,但理想情况下,表单字段将是这样的:

Expense Amount [text input]
Expense Description [text input]

My full form code can be found here: http://pastebin.com/m2b280b0f

我的完整表单代码可以在这里找到:http://pastebin.com/m2b280b0f

4 个解决方案

#1


2  

Assuming you can only add one per form (versus JS to add [n] new non labor expenses), just create an empty instance variable in the controller.

假设您只能为每个表单添加一个(与JS相比,添加[n]新的非人工费用),只需在控制器中创建一个空实例变量。

@new_non_labor_expense = NonLaborExpense.new

In the form, build it out using:

在表单中,使用以下方法构建它:

<%= f.text_field :new_non_labor_expense, :amount %>

Or whatever the appropriate member var and field type are.

或者无论适当的成员var和字段类型是什么。

In the controller, you'll get a param called "new_non_labor_expense" and can access it, validate it, associate it, and save it. Or, if any of those steps err you can bounce back and notify the user.

在控制器中,您将获得一个名为“new_non_labor_expense”的参数,可以访问它,验证它,关联它并保存它。或者,如果这些步骤中的任何一个错误,您可以退回并通知用户。

If you need to add more, just make an array of them so you pass in an array of NLEs. Hope that makes sense.

如果你需要添加更多,只需创建一个数组,以便传入一个NLE数组。希望有道理。

#2


1  

Another option would be fields_for - which seems to work just fine for me. Check out Railscasts 73 - 75.

另一个选择是fields_for - 这似乎对我来说很好。查看Railscasts 73 - 75。

#3


0  

non_labor_expenses is not a text field, which is probably your biggest problem.

non_labor_expenses不是文本字段,这可能是您最大的问题。

A better way to do this might be to create a new non_labor_expense, and in its creation have a dropdown select box to pick the id (or name, through the id) of the project.

更好的方法是创建一个新的non_labor_expense,并在其创建中有一个下拉选择框来选择项目的id(或名称,通过id)。

#4


0  

I think what you need are nested forms. Here is simple example how to do it with one to many relation. It works since Rails 2.3.2

我认为你需要的是嵌套表格。这是一个简单的例子,说明如何使用一对多关系。它自Rails 2.3.2起作用

#1


2  

Assuming you can only add one per form (versus JS to add [n] new non labor expenses), just create an empty instance variable in the controller.

假设您只能为每个表单添加一个(与JS相比,添加[n]新的非人工费用),只需在控制器中创建一个空实例变量。

@new_non_labor_expense = NonLaborExpense.new

In the form, build it out using:

在表单中,使用以下方法构建它:

<%= f.text_field :new_non_labor_expense, :amount %>

Or whatever the appropriate member var and field type are.

或者无论适当的成员var和字段类型是什么。

In the controller, you'll get a param called "new_non_labor_expense" and can access it, validate it, associate it, and save it. Or, if any of those steps err you can bounce back and notify the user.

在控制器中,您将获得一个名为“new_non_labor_expense”的参数,可以访问它,验证它,关联它并保存它。或者,如果这些步骤中的任何一个错误,您可以退回并通知用户。

If you need to add more, just make an array of them so you pass in an array of NLEs. Hope that makes sense.

如果你需要添加更多,只需创建一个数组,以便传入一个NLE数组。希望有道理。

#2


1  

Another option would be fields_for - which seems to work just fine for me. Check out Railscasts 73 - 75.

另一个选择是fields_for - 这似乎对我来说很好。查看Railscasts 73 - 75。

#3


0  

non_labor_expenses is not a text field, which is probably your biggest problem.

non_labor_expenses不是文本字段,这可能是您最大的问题。

A better way to do this might be to create a new non_labor_expense, and in its creation have a dropdown select box to pick the id (or name, through the id) of the project.

更好的方法是创建一个新的non_labor_expense,并在其创建中有一个下拉选择框来选择项目的id(或名称,通过id)。

#4


0  

I think what you need are nested forms. Here is simple example how to do it with one to many relation. It works since Rails 2.3.2

我认为你需要的是嵌套表格。这是一个简单的例子,说明如何使用一对多关系。它自Rails 2.3.2起作用