beaconhill
beaconhill

Reputation: 451

Rails: How to get value from different model

I generated two scaffolds: creditcards and creditscore.

In the creditcards credits.html.erb view, I'm trying to display the current_user's score from the creditscore model.

Here's what I've tried putting in creditcards_controller.rb:

  def credits
    @creditscores = Creditscore.find_all_by_user_id current_user[:id] if current_user
    @creditscore = current_user.creditscore
  end

And in credits.html.erb view:

<%= creditscore.score %> 

Here's the error:

undefined local variable or method `creditscore' for #<#<Class:0x00000101a69558>:0x00000101a68680>

Upvotes: 0

Views: 57

Answers (2)

skozz
skozz

Reputation: 2720

Example:

  • Model: Dogs
  • Model: Cats
  • Controller: Cats

1 - Set your global in Cats controller: @dogs = Dogs.all

2 - Use @dogs in your Cats view: <%= @dogs.cats_hunted %>

Upvotes: 0

Santhosh
Santhosh

Reputation: 29124

Only instance variables intialized in the controller is available in the views.

You should use

<%= @creditscore.score %> 

Upvotes: 1

Related Questions