Reputation: 129
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
My User Model
class User < ApplicationRecord
has_many :services
end
My Service Model
class Service < ApplicationRecord
belongs_to :user
end
Upvotes: 0
Views: 108
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