I am creating a mountable rails engine for a wrapper around a 3rd party API.
controllers:
app/controllers/myengine/v1/application_controller.rb
app/controllers/myengine/v1/customers_controller.rb
module Myengine
class V1::CustomersController < V1::ApplicationController
respond_to :json
def create
...
end
end
end
models:
app/models/myengine/customer.rb
views:
app/views/myengine/v1/customers/_customer.json.jbuilder
app/views/myengine/v1/customers/create.json.jbuilder
I am using JBuilder for my views. Whenever I hit my create action via a REST client, rails will figure out the appropriate view for it (ie. app/views/myengine/v1/customers/create.json.jbuilder).
However, when I reference the partial inside create.json.jbuilder, it complains that the template is missing.
ActionView::MissingTemplate at /v1/customers
Missing partial myengine/customers/_customer with {:locale=>[:en],
:formats=>[:json], :variants=>[], :handlers=>[:jbuilder]}. Searched in:
engines/myengine/app/views/myengine/v1/customers/create.json.jbuilder, line 1
---------------------------------------------------------------------
ruby
> 1 json.partial! #customer
As you can see its looking for the partial in myengine/customers/_customer when it should be looking in myengine/v1/customers/_customer.
I could add json.partial! 'myengine/v1/customers/customer', customer: #customer to specify where the partial is located and this works. However, typically rails magically knows where to find the partial, just not here for some odd reason.
Your help is much appreciated!!
Related
I'm using Rails 3.2.21 with JBuilder.
I have an example where I'm using an a JBuilder partial inside of a js.erb file to pre populate some fields:
var orderData = <%= raw render :partial => 'orders/orders', formats: [:json], handlers: [:jbuilder], locals: {orders: #orders} %>;
I have a weird problem where if an error is thrown in the jbuilder template, it renders a missing template error. so
If _orders.json.jbuilder looks like this
json.array! orders do |order|
json.someProperty order.a_missing_property
end
I get this:
ActionView::Template::Error (Missing partial orders/orders with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder, :coffee, :haml, :jbuilder, :riif]}
But if there is no error, this renders properly.
Any idea how my error is getting swallowed?
Update
I've created a demo app here: https://github.com/earnold/error-demo
If you load home/index you get a missing template error. If you comment out the bad line in the template, you render the template normally. What I am trying to do is make sure that errors aren't swallowed, but instead are shown on the page.
I was able to get an answer on Github here: https://github.com/rails/jbuilder/issues/40
Short version: this can be remedied by monkey patching Rails. Put this in an initializer and you're good to go.
if Rails.env.development? || Rails.env.staging?
module ActionView
class Template
protected
def handle_render_error(view, e) #:nodoc:
if e.is_a?(Template::Error)
e.sub_template_of(self)
raise e
else
assigns = view.respond_to?(:assigns) ? view.assigns : {}
template = self
unless template.source
# If an error occurs while the Jbuilder template is being rendered in
# in a nested context, you have a mismatch between the template format
# and the view context. Therefore, this block of code would raise
# a false exception (ActionView::MissingTemplate) and swallow the original
# error. This monkey patch tricks rails into thinking it was a json request
# so that refreshing the source hits the right partial
if template.formats == [:json]
view.lookup_context.formats = [:json]
end
template = refresh(view)
template.encode!
end
raise Template::Error.new(template, assigns, e)
end
end
end
end
end
Hi I am pretty new to rails and mvc, but my application has users and I want to allow someone to quickly sign up from the home page using a bootstrap modal and to allow them to sign up through a normal sign up page.
In my Users controller I have created a partial for the modal _newUserModal.html.erb and I want to use this in the index action from my Home controller.
I have read that I should load partials from another controller inside the action. So I am trying the following
class HomeController < ApplicationController
def index
render 'users/newUserModal'
end
end
This is giving me the error
Template is missing
Missing template users/newUserModal with {:locale=>[:en],
:formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/home/jeff/Development/iHaul/app/views" * "/home/jeff/Development/iHaul" * "/"
I have a feeling I am missing something small but I can't figure it out.
Ok by moving the render to the bottom of my home.html.erb it has allowed my code to work. I know this isn't the best practice but it works for now. If anyone can shed some light on the issue that would help a lot.
My advice for anyone who is using partials from another controller is do not give a partial the same name as a method. If you do you have to specifically tell render to use the partial file with
render :partial => 'controller/action'
if your careful render works normally, although the above is more readable.
OK my remaining question is wrong. After a nights sleep I realized I am not using the MVC pattern for what it is designed for. I was being to precious with using #user = User.new . If I just use that in the index function of my home controller everything works beautifully.
I have a polymorphic Review model. The namespaced model User::Library::Publication is reviewable. The reviews are created properly, but when I try to display them through a partial, Rails looks up the wrong directory.
In my view:
<%= render #review %>
I get this error:
Missing partial user/library/reviews/review with {:locale=>[:en], :formats=>[:js, :html], :handlers=>[:erb, :builder]}
Rails is apparently looking for the review partial within the namespace's directory views/user/library instead of the /views/reviews. Any idea why this is happening?
If you want to remove namespacing from the partial template path, you can set the prefix_partial_path_with_controller_namespace variable in your config/application.rb:
# config/application.rb
config.action_view.prefix_partial_path_with_controller_namespace = false
This will load partial paths as you define them (without the namespace).
You can see the original Pull Request here.
If you use name spaces you have to create folders/sub folders so Rails is not looking at the wrong place.
If you want to force the partial path just use:
render :partial => "review"
And create rename the review.html.erb file to _review.html.erb
I have a rails app (rails 3.1.3) that has a shopping cart model. I wanted to show a summary of the shopping cart in the layout so I created the partial views/carts/_cart.html.haml. My app was working fine and rendering the cart partial in every view. But when I installed devise 2.0, the partial could no longer be found for devise views. Instead, I would see the following error code when I tried to call a devise view:
ActionView::Template::Error (Missing partial views/carts/cart with {:handlers=>[:erb, :builder, :coffee, :haml], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
* "/Users/cameronnorgate/Web Development/Practice Apps/1-Camerons Tea/pgktea/app/views"
* "/Users/cameronnorgate/.rvm/gems/ruby-1.9.2-p290#pgktea/gems/devise-2.0.4/app/views"
As you can see, it searches for the partial in app/views, but doesn't go all the way into the 'carts' folder in order to find the 'cart' partial. This is weird, because the code I had in the layout view specified the exact path (see below):
%body{:class => params[:controller]}
.master_container
.master_header
.inner_header
.cart
= render :partial => 'views/carts/cart', :object => #cart
Can anyone help me understand why my call to render the partial is not being found when inside a devise view?
The short term fix I've made for this is to bring the partial code back into the full layout file - so now devise doesn't have to go searching and everything works... but that's not ideal and it's cluttering up my code.
Thanks!
You should be able to specify the partial with just:
= render :partial => 'carts/cart', :object => #cart
The views/ part of your definition is probably throwing it off. app/views is implied, so when you specify views/carts/cart, it's probably not finding a views directory under app/views.
If this page can be access from other pages . then
= render :partial => '/carts/cart', :object => #cart
Is the correct way , because if this page open in other models then 'carts/cart' will not be available like if url is ex. 'localhost:3000/products' this page will give missing partial error so / will solve the issue and as other answers, 'views' is not needed
For a plugin I want to hack the following feature into Rails:
When a (partial) template does not exist (regardless of the format) I want to render a default template.
So say I call an action 'users/index' if users/index.html.erb does not (or other format) exist, 'default/index.html.erb' should be rendered.
Similarly, If I call an action 'locations/edit' and 'locations/edit.html.erb' does not exist, 'default/edit.html.erb' should be rendered
For partials, If I call an action 'locations/index' and the template 'locations/index.html.erb' calls the partial 'locations/_location' which does not exist, it should render 'default/_object'
The solution is seek gives me access to the templates variables (e.g. #users, #locations) and information on the requested path (e.g. users/index, locations/edit). And it should also work with partials.
I have thought of some options which I'll post below. None of them are completely satisfactory.
Solution 2:
Use 'rescue_from' in ApplicationController
class ApplicationController > ActionController::Base
rescue_from ActionView::MissingTemplate do |exception|
# use exception.path to extract the path information
# This does not work for partials
end
end
Drawback: does not work for partials.
Rails 3.1 automatically looks for files in application/template.html.erb after looking in controller/template.html.erb you can see this in the Exception like so:
Missing template [controller name]/index, application/index with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:erb, :coffee, :builder]}. Searched in: * "/path/to/rails_project/app/views"
so, just put your default templates in app/views/application
I found a patch that is relatively clean, it only patches the lookup of the template which is exactly what was required in the question.
module ActionView
class PathSet
def find_template_with_exception_handling(original_template_path, format = nil, html_fallback = true)
begin
find_template_without_exception_handling(original_template_path, format, html_fallback)
rescue ActionView::MissingTemplate => e
# Do something with original_template_path, format, html_fallback
raise e
end
end
alias_method_chain :find_template, :exception_handling
end
end
Solution 1:
Monkey patch ActionView::Base#render
module ActionView
class Base
def render_with_template_missing(*args, &block)
# do something if template does not exist
render_without_template_missing(*args, &block)
end
alias_method_chain :render, :template_missing
end
end
This monkey patch requires to look into the (changing) internals of rails and results in ugly code, but probably works.