In my routes.rb I have:
resources :workouts
In my workouts controller I have:
def show
respond_to do |format|
format.html
format.json { render :json => "Success" }
end
end
But when I go to /workouts/1.json, I receive the following:
Template is missing
Missing template workouts/show, application/show with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/home/rails/app/views"
Which appears to show that the format is what it should be but it's still searching for a view. This same code functions in other controllers with identical setups just fine. Also, going to /workouts/1 for the html view seems to work just fine, though it also renders the html view properly when the format.html is removed.
Looks at the source code of render
elsif options.include?(:json)
json = options[:json]
json = ActiveSupport::JSON.encode(json) unless json.is_a?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
response.content_type ||= Mime::JSON
render_for_text(json, options[:status])
Pay attention to the third line. If the value of :json is a string, render won't call to_json automatically for this value.
So the value remains as string and render will go on to search template.
To fix, supply a valid hash even for trying purpose.
format.json { render :json => {:message => "Success"} }
You would try with /workouts.json
Related
In #show I have this code:
def show
respond_to do |format|
format.html { render :show }
format.json { #my_item.to_json }
end
end
private
def set_trip
#my_item = MyModel.find(params[:id])
end
When I'm requesting "/my_models/1.json", it throws an exception:
Showing app/views/my_models/show.json.jbuilder where line #1 raised:
Missing partial my_models/_my_model with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:jbuilder]}. Searched in:
* "app/views"
* "/home/fdsafds/.gem/ruby/2.4.2/gems/apitome-0.1.0/app/views"
Why is that? Do I have to have jbuilder? Why can't it simply convert an object to json?
You don't have to use jbuilder.
You need to call render in your json format block. See the documentation on rendering json.
2.2.8 Rendering JSON
JSON is a JavaScript data format used by many Ajax libraries. Rails
has built-in support for converting objects to JSON and rendering that
JSON back to the browser: render json: #product
You don't need to call to_json on the object that you want to render.
If you use the :json option, render will automatically call to_json
for you.
def some_action
#posts = Post.all
render partial: 'layouts/things'
end
In my layouts directory I have things as partial (_things.html.erb)
My other partials work fine. But it doesn't render partial throwing exception as
Missing partial layouts/things with {:locale=>[:en], :formats=>[:json], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in: * "/my/path/appname/app/views"
Edit-1
def some_action
#posts = Post.all
respond_to do |format|
format.json
end
end
Ok i changed my controller link this but then too same exception.
Edit-2
I have created in my views/controller_name/_some_action.json.erb
This is the controller
def some_action
#posts = Post.all
respond_to do |format|
format.json { render json: #posts }
end
end
My _some_action.json.erb file has
<%= render partial: 'shared/information', post: #posts.name %>
And created _information..js.erb in views shared directory
MY _information.js.erb has sample text as
test
I am getting json response from controller i checked with inspect element. But it is not render actual text i need (i.e., test)
well, IMHO you need to name your partial like
_things.js.erb
or no?
As Jacub Kuchar mentions, if you're trying to provide a JSON response, your partial should be named accordingly (i.e. _things.js.erb). You should also put it in the 'shared' directory rather than 'layouts' as it's a partial, not a layout.
However, I'd also keep the logic of which view to render of the controller and let the view itself handle it.
So your controller can simply say:
respond_to :json # (and whatever other response types you want to support: xml, etc)
def some_action
respond_with #posts = Post.all
end
And then having a matching view, under views/{controller name}/some_action.js.erb, which says:
<%= render partial: 'shared/things', posts: #posts %>
That way your controller isn't polluted with view logic.
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.
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