I've got a menu controller, which is set as my root controller in routes.rb. In my menu view, i try and render the _lights.slim partial with = render :partial => 'lights/lights'
but i get the following error: undefined method `lights' for nil:NilClass
MenuController:
class MenuController < ApplicationController
def index
end
end
Menu View (index.slim)
ul.tabs.vertical data-tab=""
li.tab-title.active
a href="#panel1a" Tab 1
.tabs-content.vertical
#panel1a.content.active
= render :partial => 'lights/lights'
LightsController
class LightsController < ApplicationController
before_action :discover_lights
include LIFX
@client = LIFX::Client.lan
@client.discover!
3.times do
@client.lights.refresh
sleep (0.5)
puts "Found #{@client.lights.count} with labels #{@client.lights}"
end
def index
end
def new
end
def light_toggle
light = @client.lights.with_label(params[:label])
light.on? ? light.turn_off : light.turn_on
redirect_to '/'
end
private
def discover_lights
@client = LIFX::Client.lan
@client.discover!
end
end
Lights View (_lights.slim)
h1.subheader LIFX Lights
table.light-table
thead
tr
th Light
th Status
th Power On/Off
th Brightness
tbody
-@client.lights.map do |c|
tr
th #{c.label}
th #{c.power}
th =link_to 'Toggle', light_path(:label => c.label)
th #{c.color.brightness.round(2) * 100}%
end
Routes.rb
root 'menu#index'
get '/lights', to: 'lights#index'
get '/lights/:label', to: 'lights#light_toggle', as: 'light'
I know this is a no brainer, but i'm stuck as to what to do here. I'm thinking it must be an issue with the way that when Menu#Index
is called, I never knows about my LightsController
, and so @client.blablabla will never make sense. But how will I make my app know about my LightsController
when the view is loaded as a partial