Instance variable from Rails model: undefined method `any?' -


trying display affiliate products while keeping controllers skinny possible. know why isn't working?

undefined method `any?' nil:nilclass 

app/models/shopsense.rb

require "rest_client"  module shopsense   def self.fetch     response = restclient::request.execute(       :method => :get,       :url => "http://api.shopstyle.com/api/v2/products?pid=uid7849-6112293-28&fts=women&offset=0&limit=10"     )      # !!! attention !!!     #     # works if put below in `shopsense` in controller instead     #      @products = json.parse(response)["products"].map |product|       product = openstruct.new(product)       product     end   end end 

app/controllers/main_controller.rb

class maincontroller < applicationcontroller   before_action :shopsense    def index   end    def shopsense     shopsense.fetch   end end 

app/views/main/index.html.erb

<% if @products.any? %>   <% @products.each |product| %>     <div class="product">       <%= link_to product.name %>     </div>   <% end %> <% end %> 

your index.html.erb requesting instance variable @products, isn't available through in index action of controller.

put instance variable in index action:

def index   @products = shopsense.fetch end 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -