2013-03-06 65 views
0

我想在我的應用程序中給出一個選項,讓用戶選擇最多3個自定義字段,這怎麼可能?我知道他們是一個簡單的解決方案,但不能得到。如何在rails3中添加自定義字段?

我的意思是它有可能在rails中讓用戶選擇他想要的數據中額外字段的標籤。例如我有一個表水果與字段名稱,類型,品味和兩個額外字段extra_field1和extra_field2。前三個字段是可見的窗體上具有相同的標籤和一個鏈接添加自定義字段,點擊該鏈接後,用戶得到一個表格,他可以標記他的選擇額外的領域像人A名稱他們率和重量而人B將它們命名爲速率和顏色等。名稱設置後,每次創建新記錄時,表單上的用戶應該都可以看到額外字段。

回答

1

一個解決方案可以是你可以有型號CustomField,在custom_fields表中設置可用的自定義字段。使用多選下拉菜單允許用戶選擇自定義字段,儘可能多。 而且會有一箇中間模型,它將有user_id,'custom_fields_id'和value

你的關聯可能是這樣的。

user.rb

has_many :custom_fields, :through => 'UserCustomFields' 

user_custom_field.rb

belongs_to :user 
belongs_to :custom_field 

custom_field.rb

has_many :users, :through => 'UserCustomFields' 
+0

謝謝,但用戶如何爲這些字段設置標籤 – Ravindra 2013-03-06 10:44:39

+1

您可以在'custom_fields'表中添加一個字段'field_label'。 – 2013-03-06 11:15:39

+0

我想在3個標籤字段的同一個表中添加自定義字段以避免複雜性 – Ravindra 2013-03-07 09:20:13

-1

我有加編輯一個新表數據庫包含followitn列名「custome_field」:

mysql> desc custom_fields; 
+---------------+--------------+------+-----+---------+----------------+ 
| Field   | Type   | Null | Key | Default | Extra   | 
+---------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| company_id | int(11)  | NO |  | NULL |    | 
| voucher_type | varchar(255) | NO |  | NULL |    | 
| custom_label1 | varchar(255) | YES |  | NULL |    | 
| custom_label2 | varchar(255) | YES |  | NULL |    | 
| custom_label3 | varchar(255) | YES |  | NULL |    | 
| created_at | datetime  | YES |  | NULL |    | 
| updated_at | datetime  | YES |  | NULL |    | 
+---------------+--------------+------+-----+---------+----------------+ 

而且增加了三個新的列到我的表作爲custom_field1,custom_field2和custom_field3。 現在我可以在custom_field表和類中設置標籤,我想要自定義字段並在需要的表單中使用它們。

感謝@Ramiz Raja支持我。

+1

始終接受爲您工作的答案。 – 2013-03-21 10:51:59

相關問題