JDutil / contact_us

Gem providing simple Contact Form functionality with a Rails 3+ Engine.
http://contact-us-demo.herokuapp.com
MIT License
135 stars 57 forks source link

Set another layout for contact_us page views #64

Closed RedaBenh closed 6 years ago

RedaBenh commented 6 years ago

Hi, Is it an easy way to change the layout of the page.

example of rails app:

class PageController < ApplicationController
  layout "empty"
end
RedaBenh commented 6 years ago

An other solution is to create a task to contact_us gem. The new task will be bundle exec rake contact_us:copy_controllers so we can customize controller (like devise gem)

JDutil commented 6 years ago

@RedaBenh well I think if you'd like you could add it as a configuration option, but a simpler approach could probably be to just use something like class eval. For example in your config/initializers/contact_us.rb file you could try adding to the bottom of it:

ContactUs.setup do |config|
end

ContactUs::ContactsController.class_eval do
  layout 'your_layout'
end

Let me know if that works out or you run into an error it's probably something trivial I'm missing or just a bad order in the initialization process.

RedaBenh commented 6 years ago

I just try this, i get the following error:

s/ruby-2.3.1/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:495:in `load_missing_constant': Unable to autoload constant ContactUs::ContactsController, expected /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/app/controllers/contact_us/contacts_controller.rb to define it (LoadError)
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:184:in `const_missing'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/config/initializers/contact_us.rb:31:in `block in <top (required)>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/contact_us-1.0.1/lib/contact_us.rb:25:in `setup'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/config/initializers/contact_us.rb:2:in `<top (required)>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `block in load'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/engine.rb:652:in `block in load_config_initializer'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.8/lib/active_support/notifications.rb:166:in `instrument'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/engine.rb:651:in `load_config_initializer'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/engine.rb:615:in `each'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/engine.rb:615:in `block in <class:Engine>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/initializable.rb:30:in `instance_exec'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/initializable.rb:30:in `run'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/initializable.rb:55:in `block in run_initializers'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/initializable.rb:44:in `each'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/initializable.rb:44:in `tsort_each_child'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `call'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:415:in `each_strongly_connected_component_from'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
  from /Users/redabenh/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/initializable.rb:54:in `run_initializers'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/application.rb:352:in `initialize!'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/config/environment.rb:5:in `<top (required)>'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/config.ru:3:in `require_relative'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/config.ru:3:in `block in <main>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/builder.rb:55:in `instance_eval'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/builder.rb:55:in `initialize'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/config.ru:in `new'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/config.ru:in `<main>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/builder.rb:49:in `eval'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/builder.rb:49:in `new_from_string'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/builder.rb:40:in `parse_file'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/server.rb:300:in `build_app_and_options_from_config'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/server.rb:209:in `app'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands/server.rb:61:in `app'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/rack-1.6.8/lib/rack/server.rb:337:in `wrapped_app'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands/server.rb:139:in `log_to_stdout'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands/server.rb:78:in `start'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:80:in `block in server'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:75:in `tap'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:75:in `server'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/bin/rails:9:in `require'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/bin/rails:9:in `<top (required)>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
  from /Users/redabenh/.rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/bin/spring:15:in `require'
  from /Users/redabenh/Sites/INSPINIA-WB0R5L90S/easyforce/bin/spring:15:in `<top (required)>'
  from bin/rails:3:in `load'
  from bin/rails:3:in `<main>'

i find one solution : copy the ContactsController to my local file ! but it's not a best practice :(

JDutil commented 6 years ago

Yea so that's because of trying to write the class_eval at the wrong point in the initialization process. Here is an article about the approach I'm suggesting http://www.refinerycms.com/guides/extending-controllers-with-decorators

I think you could move that class eval code to a file say: app/controllers/contact_us/contacts_controller_decorator.rb

That should hopefully call it after the contact us engines code has been initialized unless theres some other initialization issue I'm not thinking of.

RedaBenh commented 6 years ago

Great ! it's exactly what i need