klasarn
klasarn

Reputation: 129

How to pass @instance variable to my view

I'm setting up a new web application, and want to add personalized offers, if a user chose "ebay offers" for example. I only want to display the offers for ebay. How do I need to call the instance variables inside my view, to render the relevant offers?

In the past, I've tried @services.service_type or @service.service_type and nested variables for example @services.user.service_type, but nothing seems to work.

My dashboard/index.html.erb

<% if current_user.is_ebay && @services.service_type %>
...
<% end %>

My dashboards_controller

class DashboardsController < ApplicationController
  before_action :authenticate_user!

  def index
     @services = current_user.services
  end
 end

EDIT

My User Model

class User < ApplicationRecord
  has_many :services
end

My Service Model

class Service < ApplicationRecord
  belongs_to :user
end

Upvotes: 0

Views: 108

Answers (1)

widjajayd
widjajayd

Reputation: 6263

@services = current_user.services
# this will generate activerecord relation meaning couple of records
# you cannot access the column directly like @services.service_type

# to print the services you can do something like this 
<% if current_user.is_ebay %>
    <% @services.each do |service| %>
      <%= service.service_type %>
    <% end %>
<% end %>

Upvotes: 2

Related Questions