RoR: How to set the value in a collection_select from the database in the edit view (1:many relation)

不打扰是莪最后的温柔 提交于 2019-12-12 06:09:38

问题


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.

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.

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:

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>

This is what is wrong:

@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.


回答1:


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.

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>

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.



来源:https://stackoverflow.com/questions/29145982/ror-how-to-set-the-value-in-a-collection-select-from-the-database-in-the-edit-v

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!