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
Related
I am trying to build a demo app like square space. I have a preview page where an iframe loads the template that was selected. So I decided that I wont be needing the default layout (application.html.erb). I created a new folder called Design1 in views and inside it created another folder called partials. I made _header.html.erb inside of it. I created another file called home.blade.php inside the Design1 folder and tried to include the 'layouts/header' in it and it gives me this err
Missing partial Design1/_header with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/usr/share/nginx/html/fuitter-test/app/views"
this is how I am rendering the header file in home.html.erb
<%= render 'layouts/header' %>
my forlder structuer
views
-Design1
-layouts
- _header.html.erb
-home.html.erb
-other folders
And I have also done
layout false
in the controller
First of all - always downcase on file names! Never use Design or MyCoolStory, in rails we use convention over configuration and that means snake_case which is everything downcased and separated with _under_scores
To your render issues:
There is a great documentation where you'll find anything you need: http://guides.rubyonrails.org/layouts_and_rendering.html
Let me help you a little.
Rails is looking for a Layout. By default it will be expected in /app/views/layouts/application.html.erb (there is nothing wrong with keeping it named as application). A Layout is the whole HTML Frame you will need. Within the Layout there (should) always be a yieldblock.
A YieldBlock in rails is where your templates are rendered into.
so basically a layout-file can look like this (i use haml for easier reading)
%html
%head
=render "shared/head"
%body
.wrapper
%nav.navigation=render "shared/navigation"
.main_content
=yield
%footer.foot=render "shared/footer"
This means you are having 3 partial-templates in /app/views/shared named _head.html.erb, _navigation.html.erb and _footer.html.erb
That's the rails way.
Further informations
If you are planning to have a multi-design app, you should structure your views in total like
/app/views/design1
/app/views/design2
/app/views/design3
/app/views/shared
and set the lookup path in your controller like this
prepend_view_path "#{Rails.root}/app/views/#{design_path}"
def design_path
current_page.design_name
end
By then, all views will be looked up into their specified folder (Spree multi_store engine, is doing like this, for an example)
You put your layouts folder in Design1 folder, thus you should use following path to render your layout:
<%= render 'Design1/layouts/header' %>
The render structure should be starting from the /views folder.
<%= render 'Design1/layouts/header' %>
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!!
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
I am trying to render a partial on a collection with <%= render #posts %> which returns the error:
Missing partial posts/post with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}
However it works if i use <%= render :partial => 'post', :collection => #posts %>
I have _post.html.erb in the same folder which uses post variable (from posts)
Why would the former example way of rendering a partial on a collection not work, but the latter example does work?
EDIT: I should specify I'm using Rails 3.2.1
The default of to_partial_path for your objects is always scoped under a view folder for the class, so your partial must be in the posts view folder.
When you use the form render :partial => 'post' it looks in the folder of the controller you are currently under.
I suspect that you are not working in the PostsController view folder, which would explain the behavior you are seeing. If you are working in the posts view folder, then something else must be going on so if you could provide more detail that would help diagnose it further
So I have a simple Rails form that is sent to controller admin, action checkLogin. If the credentials are correct, redirect the user to a new view called main.html.erb. Now in my controller I have literally tried everything, from render 'main'; to redirect_to. But I keep getting the following error:
ActionView::MissingTemplate (Missing template admin/activate with {
:handlers=>[:rjs, :rhtml, :rxml, :builder, :erb],
:formats=>[:js, :"application/ecmascript", :"application/x-ecmascript", :"*/*"],
:locale=>[:en, :en]} in view paths "/home/xxx/xxx/xxx/rails/beta/app/views"
Is this a routing issue, or what?
Try render :file => 'admin/main.html.erb' to specify the exact file.
The problem is the render call is looking for a file that matches the name of your controller action, and it isn't finding it. So, specify the exact file and you should be ok.
FYI if you call render 'main' then it will try to render the view associated with your "main" action, which may not work if you don't have a "main" action.
Also, be sure you mean to render. If you DO have a main action and you want the stuff that's defined in that action to happen then you need to redirect_to main_whatever_path.
Have you add the 'checkLogin' to routes. try this, in your routes.rb
resources :admin do
member do
get 'checkLogin'
end
end
**NOTE : get 'checkLogin' might be post 'checkLogin' according to your case..
and inside your controller action 'checkLogin', render a partial
render :partial => 'main'