mercredi 20 avril 2016

Rails attr_accessor from different controller

Following were my home_controller:

class HomeController < ApplicationController
  def index
   @products = Product.public_active.order('random()').limit(6).includes(:product_attachments).includes(:product_reviews)
  end
end

and my product.rb model

class Product < ActiveRecord::Base
  has_many :product_attachments, dependent: :destroy
  accepts_nested_attributes_for :product_attachments, allow_destroy: true
  attr_accessor :product_display
  attr_accessor :product_overview_url

  def set_extra_attributes
    self.product_overview_url = self.product_attachments[0].attachment.medium.url
  end

  def set_cover_photo
    self.product_display = self.product_attachments.find(self.cover_id).attachment.url
    end
  end

In my home views app/views/home/_product_section.html.erb, I cant access the product_display

<div class="row">
  <% @products.each do |product| %>
    <div class="img-holder">
      <a href="<%= product_path product %>">
        <img src="<%= product.product_display %>" alt="<%= product.name %>" style="width: 100%;">
      </a>
    </div>
  <% end %>
</div>

But I can access self.product_overview_url from my app/view/products/index.html.erb page template. Is there relationship I need to make so home_controller can access product.rb model? Thanks!!

Aucun commentaire:

Enregistrer un commentaire