Biri açık, basit bir dille bana collection_select açıklayabilir mi?
collection_select
Raylar API docs ile gidiyorum ve iğrenç.
Başlığı bu
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
Ve bu sadece örnek kodu vardır:
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, :prompt => true)
Biri açıklayabilir kullanarak basit Derneği (say User
has_many Plans
Plan
ait User
) benim isteğim için kullanılan sözdizimi ve neden?
Edit 1:Ayrıca, eğer form_helper
ya da düzenli bir form içinde nasıl çalıştığını açıkladı eğer harika olurdu. Web geliştirme anlıyor, ama bir web geliştirici 'nispeten yeni' Raylar için. bunun açıklamasını hayal Sen bunu nasıl açıklıyorsun?
CEVAP
collection_select(
:post, # field namespace
:author_id, # field name
# result of these two params will be: <select name="post[author_id]">...
# then you should specify some collection or array of rows.
# It can be Author.where(..).order(..) or something like that.
# In your example it is:
Author.all,
# then you should specify methods for generating options
:id, # this is name of method that will be called for every row, result will be set as key
:name_with_initial, # this is name of method that will be called for every row, result will be set as value
# as a result, every option will be generated by the following rule:
# <option value=#{author.id}>#{author.name_with_initial}</option>
# 'author' is an element in the collection or array
:prompt => true # then you can specify some params. You can find them in the docs.
)
Veya örnek kodu aşağıdaki gibi temsil edilebilir:
<select name="post[author_id]">
<% Author.all.each do |author| %>
<option value="<%= author.id %>"><%= author.name_with_initial %></option>
<% end %>
</select>
Bu FormBuilder
ama FormOptionsHelper
belgelenen değil
Basit bir dille biri bana ziyaretçi de...
Biri bana açıklayabilir kaynak python ...
Biri bana bir dizinin boyutunu veren b...
Biri bana bu SQL enjeksiyon saldırısı ...
Biri ivy.xml bağımlılık'conf özni...