This really caught me unguarded. The stocks/index.html.haml renders without problem in browser after I installed the haml-rails gem. However, when I tried to test it using rspec/capybara
describe "StockPages" do
describe "stocks/index.html.haml" do
before {visit stocks_path}
subject {page}
it { should have_selector('table#Result') }
end
end
I got this error:
Failure/Error: before {visit stocks_path}
ActionView::MissingTemplate:
Missing template stocks/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.
How do I solve this?
Thanks
In order to render Haml templates, you need the haml gem loaded. haml-rails does call require 'haml', but if you’ve only added haml-rails to the development group in your Gemfile then haml will also only get loaded in development.
In order to fix this, you need to either move haml-rails out of any group so that it always gets loaded (and so haml also always gets loaded), or add gem 'haml' to your Gemfile (outside of any group). The first option (move haml-rails out of the development group) is likely the easiest, but you might want to leave it in the development group and explicitly add gem 'haml' in order to avoid loading unneeded code in production.
Related
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
I'm using the excellent twitter-bootstrap-rails gem. There is a helper within that gem (NavbarHelper) which is used to generate Bootstrap navbars with a Ruby helper. I want to monkey patch the gem such that the dropdown lists won't have carets.
So, I looked into the source and found the relevant method here. All I have to do is override it. I created a new file in config/initializers called navbar.rb with the following content:
NavbarHelper.module_eval do
def name_and_caret(name)
"HELLO WORLD"
end
end
Presumably, all of the dropdown titles then should be rendered as "HELLO WORLD" in my app (as referenced by the gem source). However, this is not occurring, and the gem does not appear to be monkeypatched at all.
I tried placing puts NavbarHelper.methods - Object.methods in the initializers file, and there were no results, which makes me think that Rails is not loading the gem correctly before the initializers. I have also checked and verified that the gem is not using autoload for its helpers.
Edit
What may be complicating this is the fact that my Gemfile includes the gem in the following manner:
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git', branch: 'bootstrap3'
I'm not sure if this specific versioning means the monkeypatching doesn't work.
Edit #2
It seems there is only one version of the gem on my system, so I don't think that's the issue. Also, I have tried placing require 'twitter-bootstrap-rails at the top of the initializers file, with no results.
The problem is that you patch the method on this module but the module already got included at this point. Try to define this in your application_helper.rb
def name_and_caret(name)
super("blub #{name}")
end
I generated a new model called Comment.
rails g model Comment user_id:integer content:text
rake db:migrate
Then I create a simple partial view, that I intend to call from another controller/view.
Inside of a Product show view:
.comments
h3
| Questions and Answers:
small for #{#product.name}
= render 'comments/new'
Missing partial comments/new with {:locale=>[:en], :formats=>[:html],
:handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :slim, :coffee]}.
Searched in: *
"/Users/sergiotapia/Documents/Work/foobar/app/views"
I stopped and started the Rails application and it's still refusing to detect the partial. Am I overlooking something?
I would prefer not to move the comment form to the Products folder.
Incorrect File Extension
Rename partial file to _new.html.slim. Currently html is misspelt as hmtl.
Try this out:
2.2.4 Rendering an Arbitrary File
The render method can also use a view that's entirely outside of your
application (perhaps you're sharing views between two Rails
applications):
render "/u/apps/warehouse_app/current/app/views/products/show"
Rails determines that this is a file render because of the leading
slash character. To be explicit, you can use the :file option (which
was required on Rails 2.2 and earlier):
render file:"/u/apps/warehouse_app/current/app/views/products/show"
The :file option takes an absolute file-system path. Of course, you
need to have rights to the view that you're using to render the
content.
Taken from here: http://guides.rubyonrails.org/layouts_and_rendering.html
EDIT:
Oh sure, the file name is not correct, didn't see that. ;) See Kirti Thorat's answer.
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!
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.