简体   繁体   中英

Get DB values from remote server in ruby on rails

I need to display the table values from remote db.for that I did following in the controller

      def show
        @dev = Detail.find(:all)
        #raise @devices.inspect
      end

in show.html.erb

<h1>TrDeviceDetails#show</h1>
<p>Find me in app/views/tr_device_details/show.html.erb</p>
<%= @dev %>

The page displayed the table with values.

[#<Detail UniqueDeviceID: 14448, SlNo: 609">, #<Detail UniqueDeviceID: 14448, SlNo: 610">].

I need to display only the SlNo .when I write <%=Detail.find(:SlNo)%> getting error

undefined method `to_i' for :SlNo:Symbol

You have already found all devises in show method of the controller. You need to iterate over the all records

<% @dev.each do |d| %>
  Dev id: <%= d.SlNo %>
<% end %>

Also going by the convention the column names should be in downcase with separation of _

I will suggest you should use each_with_index,it will avoid error occurence.

<% @dev.each_with_index do |page, index| %>

<% end %>

Don't change the query in your show method. Keep it as it is.

However, the above query returns the total records of a detail table in a hash and not just one.

In order to get only the SlNo , you need to loop over each record and then get SlNo for each.

So in your show html do the following:

show.html.erb

<% @dev.each do |dev| %>

<%= dev.SlNo %> #displays only the SlNo.

<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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