Issue with template path inside of a gem - ruby-on-rails

Error message:
ActionView::MissingTemplate:
Missing template gem_name/exception_mailer/exception_report with "mailer".
Searched in:
* "gem_name/exception_mailer"
This is my folder structure:
lib/
gem_name/
exception_mailer.rb # extends ActionMailer::Base
exception_mailer/
exception_report.html.erb
gem_name.rb
I can not think of why my template is not being found.

Ok, so I needed to modify my view paths array.
ActionMailer::Base.view_paths = ...
or
prepend_view_path('path')
Links:
ActionMailer 3 without rails
Using ActionMailer Without Rails

Related

app/controllers in my gem not added to autoload_paths

I created a Gem (a Devise extension). In this Gem I added routes served by a controller that I put under app/controllers folder. So my folder structure looks like:
app/
|_ controllers/
|_ my_pkg/my_controller.rb
lib/
|_my_gem.rb // somewhere in this file I added the routes.
However, I got error when I tested it:
uninitialized constant MyPkg::MyController
My best guess is that "app/controllers" of my Gem was not added to Rails autoload_paths.
I tried several solutions and none of them works:
changed gemspec: spec.require_paths = ["lib"] --> spec.require_paths = ["lib", "app/controllers"]
changed gemspec: remove line: spec.platform = Gem::Platform::RUBY # removing this line caused "app/controllers" to appear in $LOAD_PATH. But problem was not solved.
Have been struggling with it for 12 hours straight now.... :( Any hints will be really appreciated!
Found the answer: to have rails add "app" and its subfolders to autoload_paths, you need to define an Engine and make sure your Engine class is loaded:
Rails 3 controller from plugin
The error uninitialized constant MyPkg::MyController only say that module function can not be load rather that file not load.
See if your my_controller.rb defined as:
module MyOkg
class MyController < xxx
....
end
end
or
class Mypkg::MyController < xxxx
...
end

Rails 3.2.11 First Test App -- Template is Missing

I'm a newbie in ruby on rails Web-Programming. Today I tried to set up Ruby programming language and also the Rails framework. Ruby works properly, I made a first Test-Class Successfull. Only setting up my Rails framework prepares me some problem.
I made a test_app and i tried to run it.
rails new test_app
rails s
I realized that the the routes was commented in routes.rb and I uncommented it.
I changed #root :to => 'welcome#index' to root :to => 'welcome#index'.
I also realized that I do not had a controller for the page welcome/index and i created it
with rails g controller Welcome index.
But It do not work yet? Anybody an idea?
Template is missing
Missing template welcome/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "D:/Davide Giunta/Development/workspace[ruby]/test_app/app/views"
Ruby Version: 1.9.3p368
Gem Version: 1.8.24
Rails Version: 3.2.11
rails g controller will only create the controller part, you also need to create a corresponding view file, in this case, you want app/views/welcome/index.html.erb
Using scaffold (only while learning), or resource for your generator might be faster. (I usually create them all by hand these days)
The answer from Jim Deville is correct. You need to create corresponding view file of your controller's action (action_name.html.erb) and place it to views dir under subdirectory named like your controller.
See basics of Rails here http://guides.rubyonrails.org/getting_started.html.
When you want to create action with controller, view and model you should use rails g scaffold SomeName also see http://guides.rubyonrails.org/getting_started.html#getting-up-and-running-quickly-with-scaffolding.

Missing Template though routing seems to work

When I generate a new controller, under a subfolder, it now cannot find the templates, even though other controllers in the same 'structure' are working:
I have the following controller which sits in app/members/group_controller.rb (created by a rails g controller Members::Group command)
class Members::GroupController < ApplicationController
def index
render :layout => 'dashboard'
end
end
I have a template in views/members/group/index.html.erb
I have the following relevant line in routes.rb (ie leaving out some others for clarity):
namespace :members do
match '/group' => 'group#index'
end
rake routes shows me the following relevant line:
members_group /members/group(.:format) members/group#index
When I type the url http://127.0.0.1:3000/members/group, I get the Template Missing error as follows:
Template is missing
Missing template members/group/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :arb, :coffee]}. Searched in: * "/Users/mitch/Documents/Development/TME/app/views" * "/Users/mitch/.rvm/gems/ruby-1.9.2-p290/bundler/gems/active_admin-7c3e25f30224/app/views" * "/Users/mitch/.rvm/gems/ruby-1.9.2-p290/gems/kaminari-0.13.0/app/views" * "/Users/mitch/.rvm/gems/ruby-1.9.2-p290/gems/devise-2.0.0/app/views"
The routing is working to the index method, because I can eg put in a redirect and it gets acted upon, but I cannot get the template to display.
Why so?
Thanks
(Rails 3.1)
This seems to be linked to how I generate the controller in the first place.
I used upper case as follows:
rails g controller Members::Group (and tried a few other test controllers similarly, destroying them and recreating them)
When I destroyed the controller and ran the lower case equivelant:
rails g controller members::group all works fine and the templates can be found
I can't find any info elsewhere to support this though...
I observe that you render dashboard layout in groups index page please check path of dashboard .Is it in right place????
I had the exact same problem. When I used terminal to navigate to the directory and listed the files in /layouts, I had one layout file appear as a red, archived file. I have no idea why.
To fix it: simply copy&paste the code from the layout file, delete the layout file (rm "file"), and then create the same layout using the terminal via:
touch file_name.html.erb
Paste your code into the new file and it should work.

Missing Template PDFkit

I dropped the gem into the gem file, bundle installed it.
Have this in my application.rb:
require 'pdfkit'
config.middleware.use PDFKit::Middleware, :print_media_type => true
Doing rake middleware, I see 'use PDFKit::Middleware' in there.
I have this in my routes
match "/option_invoice/view_invoice/(/:id)" => "option_invoice#view_invoice"
Now when I go to http://0.0.0.0:3000/option_invoice/view_invoice/2.pdf, I got the missing template error. The page without the .pdf displays perfectly fine.
Missing template option_invoice/view_invoice, application/view_invoice with {:formats=>> >[:pdf], :locale=>[:en], :handlers=>[:coffee, :erb, :builder, :arb]}.
I'm on rails 3.2.2 if that helps.
Any help?
Basically you're just missing a template in given paths that corresponds to view_invoice.pdf.erb or view_invoice.pdf.haml or whatever you are using.
Either you create that and render whichever view you want in the PDF or you override the default template pulled by PDFKit.
I would recommend the first variant, this makes the rendered PDF independent of your views (kind of).
Cheers!

Custom partials in exception notification in Rails 3

I am trying to set up a custom partial for my exception notification mails in a Rails 3 app using the current version (2.4.0) of the exception_notification gem
The README clearly states that "you can customize how each of those
sections are rendered by placing a partial named for that part in your
app/views/exception_notifier directory [...] You can even add new sections that
describe application-specific data"
And I am exactly trying these: Altering existing sections and adding a new custom section. When just altering a section, my changed partial (app/views/exception_notifier/_session.text.erb) has no effect. When I add a new custom section, I get the following error in the log:
ActionView::Template::Error (Missing partial exception_notifier/user with {:formats=>
[:text], :handlers=>[:haml, :rjs, :rhtml, :builder, :erb, :rxml], :locale=>[:de]} in
view paths "/usr/lib/ruby/gems/1.8/gems/exception_notification-2.4.0/lib/exception_notifier/views"):
What am I doing wrong? I suspect that the view path is somehow messed up and that the exception_notifier doesn't bother to look in my /app/views/exception_notifier directory at all.
When exception_notification is used as a gem, the only view_path configured for the notifier is the gem own view path.
In order to override default section template or add you your own you will have to add your application template folder in the view path
Just add to your initializer
ExceptionNotifier::Notifier.prepend_view_path File.join(Rails.root, 'app/views')
If you have your own section partial don't forget to add it in the middleware options
Whatever::Application.config.middleware.use ExceptionNotifier,
:email_prefix => "[Whatever] ",
:sender_address => %{"notifier" <notifier#example.com>},
:exception_recipients => %w{exceptions#example.com},
:sections => %w{my_section1 my_section2} + ExceptionNotifier::Notifier.default_sections
From version 2.6.0 of the gem and forth, this is no longer needed.
That bug is already fixed, so there's no need to have that line on an initializer anymore.

Resources