Functional test unable to locate template - ruby-on-rails

I am attempting to write a test verifying my controller's action. When tested manually, the code executes as expected. However, my test raises an error because it is unable to locate a template I am attempting to render.
Snippet from my controller code:
response = {:success => true}
response[:html] = {}
response[:html][:list] = render_to_string :partial => "data_source", :locals => {:data_source => #data_source}
respond_to do |format|
format.json {render :json => response}
end
My test:
before do
#data_source = FactoryGirl.create :data_source, :facebook_fan_page, :account_id => #account.id, :user_id => #user.id
post :delete, :format => :json, :id => #data_source.id
end
it "should disable the data source" do
assert_equal false, #data_source.reload.enabled?
end
The error message:
ActionView::MissingTemplate: Missing partial data_sources/data_source,
capture/data_source, application/data_source with {:locale=>[:en],
:formats=>[:json], :handlers=>[:erb, :builder, :haml]}. Searched in:
* "/Users/me/code/my_app/app/views" * "/Users/me/code/my_app/vendor/bundle/ruby/1.9.1/gems/konacha-3.0.0/app/views"
from
/Users/me/code/my_app/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.17/lib/action_view/path_set.rb:58:in
`find'
There is definitely a partial in the app/view/data_sources directory "_data_source.html.haml".
Why can't the test find the partial? I suspect a setup issue
Thanks in advance.

As you are doing a json request, render_to_string is looking for a json template. Try this:
render_to_string :partial => "data_source", :formats => [:html], :locals => {:data_source => #data_source}

Related

Missin template on ruby on rails

I have this two methods on catalog_controler.rb
def latest
#boxes = Box.latest 5
#page_title = 'Novedades'
end
def rss
latest
render :layout => false
end
end
on app/views/catalog folder I have this xml file , rss.xml.erb
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
xml.channel do
xml.title #page_title
xml.link(url_for(:action => "index", :only_path => false))
xml.language "en-us"
xml.ttl "40"
xml.description "International Boxes"
for book in #books
xml.item do
xml.title(book.title)
xml.description("#{box.model})
xml.pubDate(book.created_at.to_s(:long))
xml.guid(url_for(:action => "show", :id => book, :only_path => false))
xml.link(url_for(:action => "show", :id => book, :only_path => false))
end
end
end
end
But it gives an missing template error:
Missing template catalog/rss, application/rss with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/user/desktop/eshop_con_CSS/app/views"
How can I show the xml file on browser?
Read about MimeResponds's respond_to method. It should look like below, please change the block content as per your requirement.
def rss
#latest = latest
respond_to do |format|
format.xml { render xml: #latest }
end
end
respond_to can adopt to various different MIMES including popular .json, which is very handy in building/working-with APIs.

Wicked_PDF templates is missing

I installed wicked PDF and modified my controller :
def show
respond_to do |format|
format.pdf do
render :pdf => "file_name"
end
format.html
end
end
Here is how i link to the pdf : compte_contrat_path(c,:format=>'pdf')
It works for html (without the format) but fail for PDF with the following error :
Template is missing
Missing template contrats/show with {:locale=>[:fr], :formats=>[:pdf],
:handlers=>[:erb, :builder, :coffee, :arb]}. Searched in: *
"/home/sylario/ruby/place_de_marche/app/views" *
"/usr/local/rvm/gems/ruby-1.9.2-p136/gems/activeadmin-0.5.0/app/views"
* "/usr/local/rvm/gems/ruby-1.9.2-p136/gems/kaminari-0.14.1/app/views" * "/usr/local/rvm/gems/ruby-1.9.2-p136/gems/devise-2.2.0/app/views"
What am I doing wrong?
Thanks to henry I now know it was related to the format of the ERB.
I have found a way to reuse my html.erb files :
First i do the following in the controller
format.pdf do
render :pdf => "file.pdf", :template => 'contrats/show.html.erb'
end
Then when i use partials i call them like this :
render :partial => 'fullpath/toview.html.erb', :formats => [:html], :locals => { :mylocal=>#something }
For Rails 7, this is what works:
format.pdf do
render pdf: "file_name", template: "posts/show", formats: [:html]
end
Notice that template no longer have .html.erb and don't forget to include 'formats'.

"Template is missing" when rendering Liquid template

I'm trying to render a liquid template that is stored in the database.
Here is my 'show' action in the controller:
def show
#organization = Organization.find_by_subdomain(request.subdomain)
#template = Liquid::Template.parse(Template.find(#organization.current_template).body)
#page = #organization.pages.find(params[:id])
respond_to do |format|
format.html { render #template.render('page' => #page), :template => false}
format.json { render json: #page }
end
end
However, when I visit the page, I get a "Template is Missing" exception with the following error (note that "testing testing" is the body attribute of the page object, which is currently the only thing being rendered in the template):
Missing template /testing testing with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee, :haml]}.
Searched in: * "/Users/ashercohen/Documents/Rails/Vocalem-Rails/app/views"
* "/Users/ashercohen/.rvm/gems/ruby-1.9.3-p194/gems/twitter-bootstrap-rails-2.1.1/app/views"
* "/Users/ashercohen/.rvm/gems/ruby-1.9.3-p194/gems/devise-2.1.2/app/views"
Why is it trying to find another template, when I've specifically pass the :template => false argument? Is there something I'm missing here? I'm trying to bypass using any template files, as it seems like they shouldn't be needed here (though am not strongly opposed if I am wrong).
Because render almost always takes a filename, while #template.render('page' => #page) contains the plain html. You should invoke render like this:
render :text => #template.render('page' => #page), :content_type => :html

Rspec2 Missing template Error

Upgraded form Rails 2 to 3. And RSpec 1 to 2. It looks like capybara/webrat is trying to find the template when I only want it to find the action. What is a possible workaround?
The error
Failure/Error: delete :read, :id => #feed_entry.id, :format => 'json'
Missing template feed_entries/read with {:handlers=>[:erb, :rjs, :rhtml, :rxml, :builder], :formats=>[:json], :locale=>[:en, :en]} in view paths "/Users/maletor/Sites/3md/app/views", ...
# ./app/controllers/feed_entries_controller.rb:37:in `read'
# ./app/controllers/feed_entries_controller.rb:35:in `read'
# ./spec/controllers/feed_entries_controller_spec.rb:200
app/controllers/feed_entries_controller#read
def read
if request.post?
#feed_entry.read_by(current_account)
elsif request.delete?
#feed_entry.unread_by(current_account)
end
respond_to do |format|
format.html { redirect_to topic_path(params[:topic_id]) }
format.json { render :nothing => :true, :status => :no_content }
format.plist { render :nothing => :true, :status => :no_content }
end
end
spec/controllers/feed_entries_controller_spec.rb:200
delete :read, :id => #feed_entry.id, :format => 'json'
It seems you can't do render :nothing => true with a status different from 200. An alternative is not using render, but using head :no_content.

render_to_string does not find partials (PDFKit controller response)

Ruby 1.8.7, Rails 3.0.4, PDFKit 0.5.0
I'm trying to create a PDF with PDFKit without using the middleware so I can disable javascript (there's an accordion action in there that hides a lot of info that should be on the PDF). However, whenever I try, it fails because it says the partials in my view (show.html.erb) are missing:
Missing partial programs/details with {:locale=>[:en, :en], :formats=>[:pdf], :handlers=>[:erb, :rjs, :builder, :rhtml, :rxml]}
If I remove the references to the partials, it works fine. I've also tried putting the partials in the same directory with show.html.erb to no avail. Here is the code in my controller's show action:
respond_to do |format|
format.html # show.html.erb
format.pdf {
html = render_to_string(:template => "show.html.erb")
kit = PDFKit.new(html, :disable_javascript => true )
send_data(kit.to_pdf, :filename => "test_pdf", :type => "application/pdf", :disposition => 'attachment')
}
end
Is there any way to do this and keep the partials?
EDIT: for now I've done this:
# config/initializers/pdfkit.rb
PDFKit.configure do |config|
config.default_options = {
:page_size => 'Legal',
:print_media_type => true,
:disable_javascript => true
}
end
This has the disadvantage of turning off javascript for every PDF I generate, but it'll do for now. Any answers on the original question of getting the partials to work still with render_to_string still appreciated.
I was running into this issue this morning and came across your question while looking for a solution.
Controller extract:
respond_to do |format|
format.html
format.pdf {
html = render_to_string(:layout => false , :action => "constitution.pdf.haml")
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/public/stylesheets/pdf.css"
send_data(kit.to_pdf, :filename => "#{#organisation_name} Constitution.pdf",
:type => 'application/pdf', :disposition => 'inline')
return
}
end
constitution.pdf.haml extract:
=render :partial => 'shared/constitution'
Error:
Missing partial shared/constitution with {:locale=>[:en, :e ...
After a while banging my head against a wall, I had a guess and changed constitution.pdf.haml to:
=render :partial => 'shared/constitution.html.haml'
I only know a tiny amount about Rails. Could it really be that (unlike my normal Haml views), PDFKit requires the file extension? That's fixed it for me!
You can also set :formats for render_to_string to prevent having to change your partial names.
html = render_to_string(:layout => false , :action => "show", :formats => :html)
This forces html, instead of pdf, format for the remained of the view rendering. Allowing you to use the same views/partials without change for HTML and PDF responses.
You should specify full path to your template I think:
html = render_to_string(:template => "my_view_folder_name/show.html.erb")

Resources