Missin template on ruby on rails - 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.

Related

Functional test unable to locate template

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}

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'.

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")

How do I make an RSS/Atom feed in Rails 3?

I'm pretty new to Rails 3, and I'm trying to make an RSS/Atom feed. I know about auto_discovery_link_tag, but what is the associated controller/action supposed to look like?
Thanks!
Auto_discovery_link_tag is a good start. A quick Google search and I found blog posts on How to Create an RSS feed in Rails. Let me fill you in on what your associated controller/action is supposed to look like:
controllers/posts_controller.rb
def feed
#posts = Post.all(:select => "title, author, id, content, posted_at", :order => "posted_at DESC", :limit => 20)
respond_to do |format|
format.html
format.rss { render :layout => false } #index.rss.builder
end
end
The name of this file should match the controller. See, below:
views/posts/feed.rss.builder
xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do
xml.channel do
xml.title "Your Blog Title"
xml.description "A blog about software and chocolate"
xml.link posts_url
for post in #posts
xml.item do
xml.title post.title
xml.description post.content
xml.pubDate post.posted_at.to_s(:rfc822)
xml.link post_url(post)
xml.guid post_url(post)
end
end
end
end
This is where all the Railsy magic happens. Here, the RSS feed XML is generated and returned to HTTP.
Using the auto_discovery_link_tag:
In the controller:
respond_to do |format|
format.html
format.atom {render action: 'index', layout: false}
end

Resources