RoR:如何在编辑视图中从数据库中的collection_select中设置值(1:许多关系) [英] RoR: How to set the value in a collection_select from the database in the edit view (1:many relation)

查看:114
本文介绍了RoR:如何在编辑视图中从数据库中的collection_select中设置值(1:许多关系)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在为一个1:n关系创建一个复杂的Rails表单,其中包含嵌套表单,并使用另一个数据表中的值进行集合选择.

I am at the moment creating a complicated rails form for 1:n relationship with nested form and collection select with values from yet another data table.

到目前为止,只要用户在更新前未选择正确的值,它就会使用collection_select的值列表中的第一项覆盖数据库值.我仍然需要在collection_select中正确设置初始值.

So far, it overwrites the database value with the first entry in the values list of the collection_select whenever the user does not select the correct value before update. I still need to set the initial value in the collection_select correctly.

我已经阅读了很多关于SO的问题,最相关的是: f-collection-select-not-displaying-the-selected-value

I have read a lot of questions on SO already, most relevant was: f-collection-select-not-displaying-the-selected-value

唯一缺少的是(我希望!)是数据库中表单字段的值的设置,因此即使用户没有选择,它也不会被选择的可选值中的默认值覆盖.触摸了选择.

The only thing still missing (I hope!), is the setting of the value of the form field from the database, so it does not get overwritten with a default value from the selects selectable values, even though the user has not touched the select.

此代码段来自我的代码和上述问题的解决方案,并且是错误的.

This snippet is derived from my code and the solution to the abovementioned question and is wrong.

让一个人拥有很多东西,而合法事物包含事物的可能值:

Let a person have many things and validthings contains the possible values for things:

在Things表中将只有Thing字符串,它们也位于有效表中.可以从事物表中为collection_select选定的参数提供一个字符串,该字符串可以在validthings表的值列表中进行标识.

In the things table there will only be Thing strings, that are also in the validthings table. It is possible to give the collection_select selected param a string from the things table that can be identified in the list of values from the validthings table.

<div class="col-md-12">
  <%= form_for(@person) do |f| %>
    <%= f.fields_for :things do |d| %>
      <%= d.hidden_field :id %><%= d.hidden_field :person_id %>
      <%= d.collection_select(:Thing, Validthings.all, :Thing, :Thing, {:selected => @person.things.map(&:id).Thing.to_s} ) %>
    <% end %>
  <% end %>
</div>

这是错误的:

@ person.things.map(&:id).Thing.to_s

是的,在表人员和事物以及事物中,该列名为事物".这是表有效项中的唯一字符串-数据库结构不是我的主意,我只能使用它.

And yes, in tables persons and things and validthings the column is named "Thing". It is a unique string in table validthings - the database structure was not my idea, I only work with it.

推荐答案

在此处找到了有用的答案:

Found a helpful answer here: rails-accessing-current-value-of-a-symbol to another subject, but my problem was that I did not know how to access the information that I knew must already be loaded.

这是我可以将collection_select的默认值指定为数据库中的数据的方法:

This is how I can specify the default value of a collection_select to be the data from the database:

<div class="col-md-12">
  <%= form_for(@person) do |f| %>
    <%= f.fields_for :things do |d| %>
      <%= d.hidden_field :id %><%= d.hidden_field :person_id %>
      <%= d.collection_select(:Thing, Validthings.all, :Thing, :Thing, {:selected => d.object.Thing} ) %>
    <% end %>
  <% end %>
</div>

其中 d.object.Thing 是属性"Thing"的表单元素的相应对象的值,该属性已经存在于表单中.

where d.object.Thing is the value of the respective object of the form element for the attribute "Thing", which is already present in the form.

如果我的做法不像是红宝石之类的话,我将非常感激建设性的想法.我对红宝石,铁轨等并不陌生.

I'd be very grateful for constructive ideas, in case my approach is un-ruby-like or some such. I am rather new to ruby, rails etc.

这篇关于RoR:如何在编辑视图中从数据库中的collection_select中设置值(1:许多关系)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆