Altair Todescatto F
Altair Todescatto F

Reputation: 133

Fetching data on rails

I need to fetch some data on rails database. The problem is, the ActiveRecord tutorials around doesn't help very much on my problem.

I need to get a Name(foreign key), email and amount of sold credits from a DB, and make a HTML Table with that. The table is a html partial render inside a Admin Area HTML. Where did a write the code to get the data and pass to view? On controller, model, i get lost on that, i tried the '@var = User.all' on controller but didn't get any result on view, something is missing and i clearly don't get the rails logic hehe

On 'home_controller', which is inside a 'admin_area' folder, i got

def top_coaches
 @var = Coach.all
end

Then, my view follows a path, where the 'routes' had home/index as root

root to: "home#index"

On index i render '_top-coaches.html.haml' as partial

  %h1 Top Coaches
  = render partial: 'admin_area/home/partials/top-coaches' 

which has this on it

%ul
  - @var.each do |coach|
    %li= coach.name

This doesn't do nothing, it renders the partial if a put some h1 or p, but i cant get the data i need to make a table

Upvotes: 0

Views: 431

Answers (1)

Rob Nice
Rob Nice

Reputation: 298

Ok, try this in your HomeController:

def index
  top_coaches
end

In your routes file, root to: 'home#index' tells rails to look for the HomeController and invoke the index method.

Since you said in the comments that you do not have that defined. It's inherited from ApplicationController, so in the code above your overriding it (at least i'm pretty sure that's what's going on, someone correct me if I'm wrong), so now your top_coaches method will be called and your @var instance variable will be set.

Upvotes: 1

Related Questions