3

Can somebody explain this ? , it is from ruby guides

<%= collection_select(:person, :city_id, City.all, :id, :name) %>

I have an attachment model, I want to select its version with combobox when I create an object, and also I want a new version option.

here what attachment has

def change
create_table :attachments do |t|
  t.string :filename
  t.attachment :file
  t.string :version
  t.text :description
  t.timestamps null: false
end

UPDATE:

<%= f.collection_select( :version,  Attachment.where.not(version: nil), :version, :version) %>

it is working like that, but I don't understand,

Ramazan Zor
  • 209
  • 1
  • 14
  • Try this - `<%= collection_select(:object, : attachment_id, Attachment.all, :id, :version) %>` – Vishnu Atrai Aug 20 '15 at 10:17
  • 1
    it said **undefined method `attachment_id'**, and I think the object is :attachment ? from **<%= form_for(@attachment) do |f| %>** – Ramazan Zor Aug 20 '15 at 10:26
  • sorry I realized know I didn't f.collection_select, I handle it now, but I don't know how to add a new version option to write something different – Ramazan Zor Aug 20 '15 at 10:33
  • by the way ı handle it but I still not understand how it works – Ramazan Zor Aug 20 '15 at 10:35
  • `<%= collection_select(:f, :attachment_id, Attachment.all, :id, :version) %>` I thought it is working but the objects have a null version – Ramazan Zor Aug 20 '15 at 10:41

2 Answers2

5

Try this to avoid the nil value of the version:

collection_select(:f, :attachment_id, Attachment.where.not(version: nil), :id, :version)

Explanation of How collection_select Works:

collection_select(
    :f, # field namespace 
    :attachment_id, # field name
    # result of these two params will be: <select name="f[attachment_id]">...

    # then you should specify some collection or array of rows.
    # In your example it is:
    Attachment.where.not(version: nil)

    # 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
    :version # this is name of method that will be called for every row, result will be set as value
)

See this and this for more information.

Community
  • 1
  • 1
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
1

check accepted answer from this thread for explanation on how collection_select works: Can someone explain collection_select to me in clear, simple terms?

In this select:

<%= collection_select(:f, :attachment_id, Attachment.all, :id, :version) %> 

you display all versions from already created attachments, so if attachments table is empty you will get null

Community
  • 1
  • 1
greg
  • 29
  • 6
  • I can see the options like 1.version, 2.version, etc. I can select but it is not working, the object I just created has null version – Ramazan Zor Aug 20 '15 at 11:23
  • if you use strong parameters check if you permit f[:attachment_id], also consider using select_tag instead of collection_select as it's sending single selected item, not an array of them – greg Aug 20 '15 at 12:43
  • I don't understand what f[attachment_id] is, I just want to choose new attachments version from combobox, I handle it like that `<%= collection_select(:f, :attachment_id, Attachment.all, :id, :version) %>` .But I don't know how to write new version instead of choosing from combobox – Ramazan Zor Aug 20 '15 at 13:20