seanrclayton
seanrclayton

Reputation: 157

How to display a value of a column from a table in Rails

So this (like all my others) is a very beginner question.

This is my routes.rb file

    Status::Application.routes.draw do
    get '/admin/status' => 'admin#apistatus'
    end

This is my controller file (called admin_controller.rb)

    class AdminController < ApplicationController
    def apistatus
    @cdn = Api.find(1,:select=>"status")
    def index
    @cdn=1
    end
    end
    end

This is my Model file Api.rb

    class Api < ActiveRecord::Base
    end

This is my erb file apistatus.erb

    <h1>Status#index</h1>
    <% if @cdn == 1 %>
    <p>Foo</p>
    <% else %>
    <p> hello </p>
    <%= @cdn %> 
    <% end %>

In my database (sqllite) I have a table called apis with the columns id status and status_message

I have 1 record with the id of 1 and status is green and status_message All systems Fine

When I navigate to mysite.com/admin/status/

I expect to see the @cdn varibale displayed as "green" but what I actually see is @cdn displayed as

    #<Api:0x007fecf4fa64a8> 

and I'm not sure why. Thanks in advance guys.

Upvotes: 0

Views: 54

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

Your column values are accessible through ActiveRecord dynamic accessors, like:

<%= @cdn.status %>

Upvotes: 1

Related Questions