#277 Mountable Engines
Aug 01, 2011 | 13 minutes | Rails 3.1
Engines are receiving a major update in Rails 3.1. You can mount them at any path, embed assets, run generators and more. See how in this episode.
- Download:
- source codeProject Files in Zip (56.6 KB)
- mp4Full Size H.264 Video (19.6 MB)
- m4vSmaller H.264 Video (12.7 MB)
- webmFull Size VP8 Video (18.7 MB)
- ogvFull Size Theora Video (23.3 MB)
Resources
- Episode 104: Exception Notification plugin for Rails 2
- Episode 149: Engines in Rails 2
- Episode 249: Notifications in Rails 3
bash
rails -v
rails plugin new uhoh --mountable
rails g controller failures index
rails g model failure message:text
rake db:migrate
rake uhoh:install:migrations
cd test/dummy
rails g controller simulate failure
rails -v rails plugin new uhoh --mountable rails g controller failures index rails g model failure message:text rake db:migrate rake uhoh:install:migrations cd test/dummy rails g controller simulate failure
uhoh/failures_controller.rb
module Uhoh
class FailuresController < ApplicationController
def index
@failures = Failure.all
end
end
end
module Uhoh class FailuresController < ApplicationController def index @failures = Failure.all end end end
uhoh/failures/index.html.erb
<%= image_tag "uhoh/alert.png" %>
<h1>Failures</h1>
<ul>
<% for failure in @failures %>
<li><%= failure.message %></li>
<% end %>
</ul>
<p><%= link_to "Failures", root_url %></p>
<p><%= link_to "Simulate Failure", main_app.simulate_failure_path %></p>
<%= image_tag "uhoh/alert.png" %> <h1>Failures</h1> <ul> <% for failure in @failures %> <li><%= failure.message %></li> <% end %> </ul> <p><%= link_to "Failures", root_url %></p> <p><%= link_to "Simulate Failure", main_app.simulate_failure_path %></p>
config/routes.rb
root :to => "failures#index"
root :to => "failures#index"
config/initializers/exception_handler.rb
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
if payload[:exception]
name, message = *payload[:exception]
Uhoh::Failure.create!(:message => message)
end
end
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload| if payload[:exception] name, message = *payload[:exception] Uhoh::Failure.create!(:message => message) end end
test/dummy/config/routes.rb
mount Uhoh::Engine => "/uhoh", :as => "uhoh_engine"
mount Uhoh::Engine => "/uhoh", :as => "uhoh_engine"
test/dummy/app/controllers/simulate_controller.rb
def failure
# redirect_to uhoh_engine.root_url
raise "Simulating an exception"
end
def failure # redirect_to uhoh_engine.root_url raise "Simulating an exception" end

