Ruby On Rails 3 - 表单 - 获取以某种方式连接到表单的嵌套数据?

发布于 2024-11-16 10:20:08 字数 872 浏览 3 评论 0原文

我有一个包含类别、名称、尺寸、价格和网址的表格。

前 3 项已填写,您应该无法更改该数据。

现在我想在输入框中显示名称而不是category_id、name、size_id。我该如何实现这个目标?

到目前为止,我得到的最接近的是使用 @size.map |s|... 创建一个“选择”输入,但这不是一个好的解决方案。我不希望用户能够更改这些数据,所以隐藏字段是我认为最好的解决方案吗?但我希望能够向他们展示有关类别、名称和尺寸的信息。

            <%= f.fields_for :price do |pr| %>
            <tr>
              <td><%= pr.hidden_field :product_id %> <%= Here I want some code showing the name of the product %></td>
              <td><%= pr.text_field :price %></td>
              <td><%= pr.text_field :url %></td>
              <td><%= pr.check_box :_destroy %></td>
            </tr>
        <% end %>

在我看来我有这些

@size     = ProductSize.all

@products = Product.all

@categories = Category.all

I have a form with category, name, size, price and url.

The first 3 have already been filled in and you shouldn't be able to change that data.

Now I want to display the names instead of category_id, name, size_id in a input box. How do I accomplish this?

The nearest I've gotten so far is to create a "select" input with @size.map |s|.... but that is not a good solution. I don't want the user to be able to change these data, so a hidden_field is the best solution I think? But I want to be able to show them the information about the category, name and size.

            <%= f.fields_for :price do |pr| %>
            <tr>
              <td><%= pr.hidden_field :product_id %> <%= Here I want some code showing the name of the product %></td>
              <td><%= pr.text_field :price %></td>
              <td><%= pr.text_field :url %></td>
              <td><%= pr.check_box :_destroy %></td>
            </tr>
        <% end %>

In my views I have these

@size     = ProductSize.all

@products = Product.all

@categories = Category.all

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一抹苦笑 2024-11-23 10:20:08

我不确定我完全理解你的控制器是如何工作的,但无论如何你应该能够获得产品、尺寸和类别。如果您能够设置它们的 ID,那么您可能已经获得了它们。在您的控制器中,只需将产品、尺寸和类别设置为变量即可。

所以,在你的控制器中:

@category = YourCategory
@product = YourProduct
@size = YourSize

然后在你的表单中的某个地方你可以做类似的事情:

<%=h @category.name %>

I'm not sure I fully understand how your controller works, but regardless you should be able to get the product, size, and category. You probably have already got them if you were able to set their IDs. In your controller just set the product, size, and category to a variable.

So, in your controller:

@category = YourCategory
@product = YourProduct
@size = YourSize

Then in your form somewhere you can just do something like:

<%=h @category.name %>
静若繁花 2024-11-23 10:20:08
<%= Here I want some code showing the name of the product %>

现在

<%= pr.object.name %>

请注意“对象”并不代表某些东西。只需按键入的方式使用

<%= Here I want some code showing the name of the product %>

is now

<%= pr.object.name %>

note 'object' does not represent something. just use as typed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文