I have a problem when trying to render a controller action. Following the documentation I should be able to use:
render 'user/new' or
render template: 'user/new' or
render :action => "new", :controller => "users"
Although I get a template missing exception and I'm not shure, why. Using a form for works, but it's stupid to copy exactly the same form.
I'm pretty messed, so I'm shurely missing something, but I don't get it.
Any hints?
EDIT: I'm calling from the GroupsController where I want to render the new-user-form. Did a test with only scaffolded models and I get the same error.
ActionView::Template::Error (
Missing partial users/new with {:locale=>[:de], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* "/Users/rob/Development/projects/test/app/views"
* "/Users/rob/.rvm/gems/ruby-1.9.3-p362/gems/oembed_provider_engine-0.2.0/app/views"
* "/Users/rob/.rvm/gems/ruby-1.9.3-p362/gems/devise-2.2.3/app/views"
):
Partial templates' filename always begins with an underscore. So you need a "_new.html.erb" file in your "user" view to render.
If I'm not mistaken, the default scaffolding creates a "_form.html.erb" for each model to render it in new and edit actions both. You can just render that instead of the whole "new" view.
Related
I have the following in my views/patients/show.html.slim
== render 'era/header'
Of course, views/patients/era/_header.html.slim exists, though it throws a missing template error:
ActionView::MissingTemplate at /patients/12345
Missing partial era/header with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :slim, :haml]}.
Searched in: * "/home/pablo/code/rails/tarjira/app/views"
If I use == render 'patients/era/header' works, same with == render 'era_header' (assuming I have a views/patients/_era_header.html.slim file). The latter makes me think that rails search the actual directory (views/patients), so I don't understand why in the first case I have to prefix with patients/.
I'm using Rails 4.0.4.
To render a partial as part of a view, you use the render method within the view:
== render 'era_header'
This will render a file named _era_header.html.slim at that point within the view being rendered.
== render 'era/header'
This code will pull in the partial from app/views/era/_header.html.slim. Notice how the Rails is forming the path i.e, by prefixing app/views before the given path in render method call i.e., era/header. This is how render method is implemented in Rails.
Read the Rails Guide explanation for Naming Partials
The desire for partial rendering with relative paths seems to have a long history. There's an issue from 2011 and a pull request from 2015.
For now if you just need 1 extra level as described in your question you can place a callback in your application_controller.rb:
class ApplicationController < ActionController::Base
before_action :_append_view_path
def _append_view_path
append_view_path("app/views/#{controller_path}")
end
end
This way your views will gain the ability to use render('subfolder/partial') instead of render('controller/subfolder/partial').
I am new in rails and I want to implement chat in my rails app
following http://railscasts.com/episodes/260-messaging-with-faye,
but i m unable to render
controller :
def index #messages = Chat.all // all available chats
I get the following error:
Missing partial chats/chat with {:handlers=>[:builder, :erb, :coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
* "/home/swagata/Desktop/swagata_new/swagata/app/views"
* "/home/swagata/.rvm/gems/ruby-1.8.7-p160#swagata/gems/devise-2.0.4/app/views"
I tried creating a partial name _chat.js.erb, but with no luck.
Any solutions?
Rails is trying to render an HTML snippet, but the only partial you've provided is marked as being a Javascript snippet.
You probably want an HTML-erb partial called _chat.html.erb
Create chat.erb and check if it's working or not
If you want to use a json call to render messages;
in your controller
def index
#messages = Chat.all
respond_to do |format|
format.js { render "chat" }
end
end
and in your view file, there should be a chat.js.erb file without underscore.
And your chat.js.erb may contain for example;
$('#chat').html("<%=j render '/messages' "); line to render messages at the div with id "chat".
and at the same directory there should be a _messages.html.erb file to render #messages.
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'