简体   繁体   English

铁轨上的红宝石下拉列表

[英]dropdown list in ruby on rails

I want to display values to drop down list from Database. 我想从数据库中显示值到下拉列表。 For that in my controller class i did the following to get the values from db and its getting properly. 为了在我的控制器类中,我做了以下操作以获取db及其正确获取的值。

 @value = Message.find(:all)

<Message ID: 14448, SlNo: 609">, #<Message ID: 14448, SlNo: 610">

How can i display the SlNo values to drop down list.Here is the code am using and getting error!I don't know how to set values inside a collection_select.Please help me!! 如何显示SlNo值到下拉列表。这是代码使用和获取错误!我不知道如何在collection_select中设置值。请帮助我!

<% @value.each do |d| %>
<%=collection_select(:value, :id, @value, :id, { selected: params.fetch(:value, {})[:id].to_i, :prompt => "-Select a device" }) %>
<% end %> 

Table names getting 表名得到

["UniqueDeviceID", "SlNo"] 

Model 模型

class MessageDetail < ActiveRecord::Base
  # attr_accessible :title, :body

  set_table_name 'DeviceDetails'
set_primary_key 'SlNo'
end

instead of 代替

<% @value.each do |d| %>
  <%=collection_select(:value, :id, @value, :id, { selected: params.fetch(:device, {})[:id].to_i, :prompt => "-Select a device" }) %>
<% end %> 

use 采用

<%= collection_select :value, :id, @value, :id, :S1No, { selected: params.fetch(:device, {})[:id].to_i, :prompt => "-Select a device" } %>

UPDATE: explanations for passed parameters 更新:传递参数的解释

  • :value = a symbol representation of the record you want to update, it may also not be an instance record but just a symbol that will be used in the naming convention of the select tag :value =要更新的记录的符号表示,它也可能不是实例记录,而只是将在select标记的命名约定中使用的符号
  • :id = the column that you wish to update :id =您要更新的列
  • @value = the collection to show the choices @value =显示选项的集合
  • :id = the method you want to use that will be passed as the value of the selected value :id =要使用的方法,将作为所选值的值传递
  • :S1No = the method that will be used as the label for the options of the select tag :S1No =将用作select标签选项标签的方法
# START
f.collection_select :id, Message.all(:order => "name"), :id, :name, :include_blank => true
# END

OR 要么

# START
messages_arr = []    
messages = Message.all(:order => "name")
messages.each do |msg|
 messages_arr << [msg.name, msg.id]
end
f.select(:id, options_for_select(messages_arr), {:include_blank => 'Include All'}, {:class=>"span12"})
# END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM