ActionView::MissingTemplate in rails 3 - ruby-on-rails

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.

Related

Template is missing error when rendering html in Rails controller action

I'm attempting to use render html: to render raw html from a controller action:
class SomeController < ApplicationController
def raw_html
render html: '<html><body>Some body text</body></html>'
end
end
However, when I run this controller action, I get a "Template is missing" error
I don't want to use a template, just render raw html.
The error I get is:
Processing by SomeController#raw_html as HTML
Parameters: {}
ActionView::MissingTemplate (Missing template some_controller/raw_html
with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder,
:raw, :ruby]}. Searched in: *
"/Users/doved/source/sample_app/app/views" *
"/Users/doved/.rvm/gems/ruby-2.0.0-p353#syp/gems/chameleon-0.2.4/app/views"
* "/Users/doved/.rvm/gems/ruby-2.0.0-p353#syp/gems/kaminari-0.15.1/app/views"):
app/controllers/some_controller.rb:14:in raw_html'
lib/middleware/cors_middleware.rb:8:incall'
I'm using Rails 4.0.2
What am I doing wrong?
html option was added to render method in Rails 4.1 version.
Checkout the discussion on this topic on Github
If you upgrade the Rails version to Rails 4.1 then you would be able to render html as
def raw_html
render html: '<html><body>Some body text</body></html>'.html_safe ## Add html_safe
end
With the current version of Rails 4.0.2, you would need to use
def raw_html
render text: '<html><body>Some body text</body></html>'
end
You are getting error as: ActionView::MissingTemplate
Because currently html option is not supported by render so the value passed with html option is ignored and Rails starts to look for a template some_controller/raw_html in views directory.
Possible duplicate of
How to return HTML directly from a Rails controller?
This should work for you:
render text: '<html><body>Some body text</body></html>'

Missing template for render user/new

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.

Returning: A full page, JSON, and a partial HTML snippet from Ruby on Rails Controller

I know this question has been asked in part a few other times on SO but I was curious about doing it a different way. In my Ruby on Rails app I have an action called list on my UsersController.rb controller. I want this list to respond to 3 different things
The page itself. Rending the whole page of users I specify
A JSON list of users for the page I specify
A partial view of just the rows for the page I'm specifying formatted as HTML.
Imagine a full page (header, footer, everything) with a table that has page 1 of users. When I click page 2 I want to kick off an ajax request back to the same controller action to give me just the html rows for page 2. I also want to persist my JSON API still allowing my controller to return JSON lists when asked. I imagine it looking someting like this.
class UsersController < ApplicationController
def list
respond_to do |format|
format.html # RETURNS MY VIEW
format.json # RETURNS MY JSON LIST
format.partial_html # RETURNS MY PARTIAL HTML
end
end
end
Is there anyway to accomplish this in RoR? Or am I doomed into having to create another action in my controller just to return technically the same data?
Could I make this happen by specifying my own MIME type? Should I snake in the partial as an XML return type?
Use format.js on the third line.
Put the partial html on a partial, call it app/views/users/_html_rows.html.erb.
render that partial both on the full html and on the js version.
You will have app/views/users/list.html.erb with the full html content, which will be something like this:
<html>
<body>
.....
<table id="my_table"><%= render 'users/html_rows', users: #users %></table>
</body>
</html>
You will have app/views/users/_html_rows.html.erb with:
<tbody>
<% users.each do |user| %>
<tr>
<td>user.name</td>
</tr>
</tbody>
Then you will have app/views/users/list.js.erb with:
$("#my_table tbody").html("<%= render 'users/html_rows', users: #users %>");
This probably will solve your problem.
You can add an additional mime type entry to work with respond_to. In config/initializers/mime_types.rb, add:
# htmlp means "html partial"
Mime::Type.register "text/html", :htmlp
In your controller you can now do:
def list
respond_to do |format|
format.html
format.json
format.htmlp { render layout: nil }
end
end
And create a template called list.htmlp.erb with your partial content in it.

Rails 3.1.0 kaminari ActionView::Template::Error inside ajax request?

I'm trying to render my pagination links inside an ajax request with kaminari and im getting a server error. I'm using the render_to_string method to render the pagination links to a string then parse it via json. I'm using rails 3.1.0.
ActionView::Template::Error (Missing partial kaminari/paginator with {:handlers=>[:erb, :builder, :haml], :formats=>[:json], :locale=>[:en, :en]}. Searched in:
Basically it's looking for the partials in all my load paths and can't seem to find the files, and they're there for sure.
Has anyone experienced similar behavior and know of a possible reason?
I just ran into this as well. I was able to work around it by moving render_to_string into a respond_to block -
respond_to do |format|
format.js do
foo = render_to_string(:partial => 'some_kaminari_view').to_json
render :js => "$('#foo').html(#{foo})"
end
end
See here: http://whowish-programming.blogspot.com/2011/07/stupid-rails3-with-missing-template-and.html
Just append .html to your view name.

Rails 3 Routes/Render Missing Template

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'

Resources