![](/img/trans.png)
[英]In Rails how do I test modifications to an instance of a Model that are made in a controller?
[英]How do I use the select tag with an array made in the controller in Rails?
我在某個模型的控制器中制作了一個數組。 我將該數組存儲在變量@students中。 我無法填寫選擇下拉列表。
我目前的代碼是:
= select :student_name, :id, @students
我希望能夠按照他們的名字列出學生,這可以在student.name上調用,我希望將每個學生的價值設置為學生。
任何幫助都會很棒,因為我是rails的初學者。 順便說一下,我正在使用Rails 4
您可以使用options_from_collection_for_select
嘗試獲取活動記錄中的所有學生姓名和ID,無需在數組中分配。
<% options = options_from_collection_for_select(@students, 'id', 'name') %>
<%= f.select :all_val, options %>
如果你真的想在數組中做,那么按照以下方式做。
@students = [["john", 1], ["mandos", 2], ["kendy", 3]]
<% options = options_from_collection_for_select(@students, 'id', 'name') %>
<%= f.select :all_val, options %>
AS @Bharat soni建議,使用option-from_collection_for_select會很棒! 雖然如果你不想要它,你可以簡單地在select
標簽中列出這些選項
= select :student_name,@students,'id','name' #Would give the list of student names in the dropdown
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.