Missing Template Rails - ruby-on-rails

When I run my code on Cloud9 IDE, the next error appears:
Missing partial submissions/_submission with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/lib/action_dispatch/templates" * "/home/ubuntu/workspace/app/views" * "/usr/local/rvm/gems/ruby-2.3.0/gems/web-console-2.0.0.beta3/app/views"
I have a Submissions Controller and my create code is like this:
def create
#submission = Submission.new(submission_params)
respond_to do |format|
if #submission.save
format.html { redirect_to root_path}
format.json { render :show, status: :created, location: #submission }
else
format.html { render :new }
format.json { render json: #submission.errors, status: :unprocessable_entity }
end
end
end
I don't know which is my error and where to locate it.
Plus, a full stack text http://pastebin.com/YnyQeetU
The code of index.html.erb http://pastebin.com/fZbXd0Wk

This line of code is causing the problem.
<!--<%= render #submissions %>-->
#<!-- --> This is used to comment an HTML tag
If you do not want to render the partial you can simply comment that out as follows:
<%#= render #submissions %>
#<%# %> This is the way to comment embedded ruby code

Though the line is commented in HTML, the server tag is still being executed. Try to comment the server tag with <%#= link_to 'New Submission', new_submission_path %>. This will stop executing the server tag and you will not get the error.
More reference: How to comment lines in rails html.erb files?

Related

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

Rails: Template is missing when AJAX and .js.rjs files

I am trying to execute an AJAX routine with rails
the code runs normally but the response doesnt..
I get this error
Template is missing
Missing template line_items/create, application/create with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "D:/ruby/depot/app/views"
I created the create.js.rjs inside line_items directory like the book Agile Web Development says it is to be, but the error insists..
#file line_items_controller.rb
def create
#cart = current_cart
product = Product.find(params[:product_id])
#line_item = add_product(#cart, product.id)
respond_to do |format|
if #line_item.save
format.html { redirect_to store_url }
format.js
format.json { render json: #line_item, status: :created, location: #line_item }
#...
end
end
end
and inside my create.js.rjs I got this
page.replace_html('cart', render(#cart))
Rjs is not a valid format anymore I guess. Use js.erb instead. Also manual you're following is outdated since none uses prototype anymore.

How can I render an HTML view while constructing a JSON response in Rails?

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

"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

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