Reputation: 6259
I tried to execute some ruby code on each page of my application! I putet the hole code into my application controller:
class ApplicationController < ActionController::Base
protect_from_forgery
if Setting.exists?(1)
@setting = Setting.find(1)
else
redirect_to new_setting_path
end
end
This somehow wont work! The strange thing is that when i put the hole code into my application html it works:
<body>
<% if Setting.exists?(1)
@setting = Setting.find(1)
else
redirect_to new_setting_path
end %>
What do i have to change in my application controller?
Upvotes: 2
Views: 358
Reputation: 7937
ApplicationController is the correct place, but you should put your code in a before_filter :
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :ensure_setting
private
def ensure_setting
@setting = Setting.where( id: 1 ).first or redirect_to( new_setting_path )
end
end
Upvotes: 4