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
Related
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
I have a controller method that returns JSON. But sometimes part of that JSON object should include a rendered, serialized HTML view. So my controller method has a line like this:
html = render_to_string :partial => 'foo/bar'
# ...
render json: {x: 'y', html: html}
But that fails because Rails is only looking for JSON views!
ActionView::MissingTemplate (Missing partial foo/bar with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee, :slim, :haml]}. […]
How can I solve this?
Update: I have gotten one "level" of layout to render_to_string as html using the below syntax, but the same error persists when that layout renders its own partials!
html = render_to_string :partial => "foo/bar.html.haml"
Surely there’s a solution here, right?
Update 2: render_to_string :action => 'method_in_this_controller' seems to be doing the trick.
Repeating Yanhao's answer because I ran into this exact problem and its what worked for me.
try:
html = render_to_string :partial => 'foo/bar', :formats=>[:html]
I am writing something like that in my action:
def show
...
respond_to do |format|
format.js { render :json => data }
end
end
May be it will help you.
Are you sure you have a file 'apps/views/foo/_bar.*'? I was able to render HTML as a JSON parameter with How do I render a partial to a string?
respond_to do |format|
format.html { redirect_to post_path(post) }
format.js {
render json: {
error: flash[:error],
content: (render_to_string partial: '/comments/comment', locals: {comment: comment}, layout: false )
}
}
end
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
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.
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