如何在Rails表单中显示相关模型的数据

时间:2022-02-06 06:32:33

I have two models

我有两个型号

class Car
    has_many :engines                         
end

class Engine
    belongs_to :car
end

In the car form I have a select field where the user can select the engine type. The list might be "1.4L; 1.6L; 2.0L..."

在车辆表格中,我有一个选择字段,用户可以在其中选择引擎类型。列表可能是“1.4L; 1.6L; 2.0L ......”

Lets say I want to display additional information from the Engine model when the user selects a engine. This should be displayed on the Car form. This might be e.g. BHP, max revs, ...etc

假设我想在用户选择引擎时显示Engine模型中的其他信息。这应该显示在Car表格上。这可能是例如必和必拓,最大转速,等等

How do I set something like this up. I guess there are two aspects:

我该如何设置这样的东西。我想有两个方面:

  1. How to display data from the engine model on the car form without using a field (this data is not editable).

    如何在不使用字段的情况下在汽车表单上显示引擎模型的数据(此数据不可编辑)。

  2. How to update this data dynamically when the user selects an option in the select field.

    当用户在选择字段中选择一个选项时,如何动态更新此数据。

Can anyone point me towards a starting point for this. I'm a bit lost where to begin.

任何人都可以指出我的出发点。我有点迷失在哪里开始。

Many thanks!

非常感谢!

2 个解决方案

#1


0  

If you're working with collection_select in your form, you are setting two arguments, like :id and :name in your collection_select call. :id is the method called to determine the value for the option tag and :name is the method used to display the option tag's content.

如果您在表单中使用collection_select,则需要在collection_select调用中设置两个参数,例如:id和:name。 :id是用于确定选项标记的值的方法,并且:name是用于显示选项标记内容的方法。

Solution: Create a method (e.g. :name_for_select) in your engine model which returns a string with more information about your engine and call collection_select with :id, :name_for_select instead.

解决方案:在引擎模型中创建一个方法(例如:name_for_select),该方法返回一个字符串,其中包含有关引擎的更多信息,并使用:id,:name_for_select调用collection_select。

#2


0  

This is called a nested form, and if you google that you will find a lot of hints and tips. E.g. check out this and this tutorial.

这称为嵌套表单,如果你谷歌,你会发现很多提示和技巧。例如。看看这个和本教程。

You should also consider using a form builder like formtastic or simple_form: they have a lot of helpers to make life easier for you.

您还应该考虑使用formtastic或simple_form之类的表单构建器:它们有很多帮助程序,可以让您的生活更轻松。

#1


0  

If you're working with collection_select in your form, you are setting two arguments, like :id and :name in your collection_select call. :id is the method called to determine the value for the option tag and :name is the method used to display the option tag's content.

如果您在表单中使用collection_select,则需要在collection_select调用中设置两个参数,例如:id和:name。 :id是用于确定选项标记的值的方法,并且:name是用于显示选项标记内容的方法。

Solution: Create a method (e.g. :name_for_select) in your engine model which returns a string with more information about your engine and call collection_select with :id, :name_for_select instead.

解决方案:在引擎模型中创建一个方法(例如:name_for_select),该方法返回一个字符串,其中包含有关引擎的更多信息,并使用:id,:name_for_select调用collection_select。

#2


0  

This is called a nested form, and if you google that you will find a lot of hints and tips. E.g. check out this and this tutorial.

这称为嵌套表单,如果你谷歌,你会发现很多提示和技巧。例如。看看这个和本教程。

You should also consider using a form builder like formtastic or simple_form: they have a lot of helpers to make life easier for you.

您还应该考虑使用formtastic或simple_form之类的表单构建器:它们有很多帮助程序,可以让您的生活更轻松。