Reputation: 3920
I want to populate values fetched from a database to a drop down list, using rails 3:
@details
contains all the values from the database. While printing @details.inspect
it displays in the web page as:
[<DeviceDetail DeviceID: 14448, No: 616">, <DeviceDetail DeviceID: 14448, No: 617">, <DeviceDetail DeviceID: 14448, No: 618">]........
In the loop I fetch the No
details. How can I show No
in a drop down list?
<% @details.each do |d| %>
<%=d.No%>
<% end %>
I added the following for displaying No
in drop down list, but it's returning the error undefined method name for DeviceDetail:0x390f3f0
. My database does not contain name filed, so what should I put in the :name
field? The database table contains no
, deviceid
, speed
and time
.
<% @details.each do |d| %>
<%=collection_select(:device_detail, d.No, @details, :id, :name) %>
<% end %>
It this possible?
Upvotes: 0
Views: 2228
Reputation: 291
Is this part of a form? Use the select_tag
, and select the input option for the same.
Check out http://apidock.com/rails/ActionView/Helpers/FormTagHelper/select_tag.
Upvotes: 2
Reputation: 3800
If I understand your question correctly, you will need to declare the instance variable @details
in your Device
controller. It should look something like this:
controller/device.rb:
def show
@details = Detail.all
end
Then, in your index and show pages, you can access that instance variable like this:
views/device/show.rb:
<% @details.each do |detail| %>
<%= render detail %>
<% end %>
It should loop through each of your details and render each one. If you only want to show the the "no" of details it should look like this:
<% @details.each do |detail| %>
<%= detail.no %>
<% end %>
Or, if you're on the show page you could simply call the detail for that page like this:
<%= detail.no %>
Is this what you were asking?
Upvotes: 0