Rspec2 Missing template Error - ruby-on-rails

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.

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.

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}

If there is no need to response in remote form how to handle it on Rails 3?

If there is no need to response in remote form how to handle it?
the error is
ActionView::MissingTemplate (Missing template carts/search_book_by_sn, application/search_book_by_sn with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee, :haml]}. Searched in:
The code in my controller.
Because the
if session[:loaded_books].include? #book.id
is a condition for false-result, so I don't want to execute any Javascript. Just keep it as same as origin
respond_to do |format|
# format.js
if session[:loaded_books].include? #book.id
format.js
else
ap("Add into Array")
session[:loaded_books] << #book.id
ap(session[:loaded_books])
format.js { render :action => 'add_to_cart'}
end
end
Thanks in advance
If you don't want to send any response or render any view then the write in action
render :nothing => true
respond_to do |format|
# format.js
if session[:loaded_books].include? #book.id
format.js {render :nothing => true } # this might help
else
ap("Add into Array")
session[:loaded_books] << #book.id
ap(session[:loaded_books])
format.js { render :action => 'add_to_cart'}
end
end
simply do render :nothing => true in your controller

getting "ActionView::MissingTemplate" while rendering a simple json head response

In one of my controllers, I have this code:
respond_to do |format|
format.html{ redirect_to :me, :flash => {:error => t('quest_histories.misc.bad_request')}} and return
format.json{ head :method_not_allowed } and return
end
BUT, when a json request comes, i get this error:
ActionView::MissingTemplate (Missing template quest_histories/index, application/index with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/var/www/PMAC_RoR/app/views"
This really confuses me, because I have similar code in many other controllers and it's actually working... the controller just have to respond with a html header, it shouldn't need a template.
Instead of "and return" remove those, and put the return after the whole respond_to block.
Try adding this:
render :nothing => true
respond_to do |format|
format.html{ redirect_to :me, :flash => {:error => t('quest_histories.misc.bad_request')}} and return
format.json { render :nothing => :true, :status => :no_content }
end

rails 3, scaffolded app, how add own error message to update method?

In a scaffolded Rails 3, when 'update' method fails to save, the logic is already there to redirect back to the edit page.
we modified the scaffolded method to do some custom validation logic (after update, but before we render the resulting view
respond_to do |format|
if #thingy.update_attributes(params[:thingy])
if #thingy.found_warning_101
WHAT GOES "HERE" TO REDIRECT TO EDIT PAGE
AND HAVE THE DEFAULT SCAFFOLDING ERROR HANDLING SHOW "WARNING 101"?
THIS DOES NOT WORK, GIVES MISSING VIEW ERROR, DOESNT FIND EDIT VIEW:
format.html { render :action => "edit", :notice => "WARNING 101" }
format.xml { render :xml => #things.errors, :status => :unprocessable_entity }
return
end
format.html { redirect_to(#thingy, :notice => "thingy was successfully updated.") }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => #beep.errors, :status => :unprocessable_entity }
end
We tried (above) to simply copy the same code that scaffolding creates for the case that .update_attributes fails (followed by return) but we get a missing view error:
Missing template thingys/update with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml, :haml, :voice], :formats=>[:html], :locale=>[:en, :en]} in view paths
I don't really understand why you want to do this, anyway, here is the way to achieve it:
#thingy.errors[:base] << "whatever text you want"
In you controller of course.
By the way, the missing template appears to be update, not edit

Resources