ActionController::UnknownFormat / Missing template error - javascript

I am getting a "template missing error" on a .js.erb file, everything i've read says to add a respond_to which I did below but it's still not seeing my create.js.erb file, any ideas?
def create
#conversation = Conversation.find(params[:conversation_id])
#message = #conversation.messages.build(message_params)
#message.user_id = current_user.id
#message.save!
#path = conversation_path(#conversation)
respond_to do |format|
format.js
end
end

Seems like you don't have create.js.erb file in app/views/:controller_name/
If not create one at app/views/:controller_name/

Related

Rails respond_to throw ActionController::UnknownFormat

So, I'm trying to respond to an action with a js file.
def my_schedule
respond_to do |format|
format.js
end
end
In my view, I have 'my_schedule.js.erb' but it's not even executed, rails broke in the controller and throw me an error : ActionController::UnknownFormat, where I have the respond_to.
I tried to add
respond_to :js, :json, :html
at the beginning of my controller out of the actions but still not working.
Need help to debug this and understand how respond_to really works.
format.js will only respond to an xhr request. You can't trigger this response by just navigating to the route that points to this controller and method.
You can test the js.erb execution by changing the respond_to block to
def my_schedule
respond_to do |format|
format.html
format.js
end
end
Then create a my_schedule.html.erb file in the same view folder as the js.erb with the following contents
<%= link_to 'Test', my_schedule_path, data: { remote: true } %>
Note that you may need to adjust that path, I'm just guessing on that.
Then navigate to the same path you were trying to before. You should see a link which, when clicked, will fire the js response.

How to render only js without html (template, layout) in Rails

I have controller Message_Controller and this Controller has method "message" in this method i wanna render .js.erb file i need call js function from rails controller.I need reneder it without html-template(layout) just only js-code in this code i will call js-function with args .How to make it ??
My routes:
post 'chat_bot/message', to: 'chat_bot#message'
My controller:
class ChatBotController < ApplicationController
layout false
def message
#gon.watch.message = params[:text]
#message = params[:text]
puts #message
render partial: 'message.js.erb', layout: false
end
end
my message.js.erb file
alert('<%=#message %>');
With
render partial: 'message.js.erb', layout: false
Rails is going to look for a partial called _message.js.erb right in the folder responding to that controller.
You can use respond_to and there specify the format and what to render:
def message
respond_to do |format|
format.html { render partial: 'message.js.erb' }
end
end
You can skip the instance variable assignation if you prefer, as you have access to the params within the request.
If your idea is to "evaluate" the alert, then it still should be inside a script tag:
<script>
alert("<%= params[:text] %>");
</script>
just update it with
def message
#message = params[:text]
respond_to do |format|
format.js
end
end

Uncaught SyntaxError: Unexpected token < in rails

This is my first question on StackOverflow. I am new to Rails and am making a simple Rails app in which I am doing a modal popup for user login in. My code is below.
App/Controller/Sessions:
class Users::SessionsController < Devise::SessionsController
# respond_to :html, :json
# before_action :check_user_session, only: [:new]
# GET /resource/sign_in
def new
self.resource = resource_class.new(sign_in_params)
clean_up_passwords(resource)
yield resource if block_given?
respond_to do |format|
format.js
format.html
end
end
# POST /resource/sign_in
def create
self.resource = warden.authenticate(auth_options)
if self.resource.present?
set_flash_message(:notice, :signed_in)
sign_in(resource_name, resource)
yield resource if block_given?
respond_with resource, location: after_sign_in_path_for(resource)
else
respond_to do |format|
format.js
end
end
end
My new.js.haml file:
$("#login-modal").html("#{escape_javascript(render 'new')}");
$("#exampleModal").modal();
I am getting this error when I click the sign in button.
change file new.js.haml to new.js.erb with following code:
$("#login-modal").html("<%= escape_javascript(render 'new') %>");
$("#exampleModal").modal();

rendering multiple files - rails

Suppose I make an ajax a call, from which I want to get some static template (app/views/static/some_template.html.erb), on which I want to act with some javascript stored here app/views/layouts/sign_in.js.erb. Is it possible to render multiple file ? (because I want to keep separate my js files and my html files)
def ajax_call
respond_to do |format|
...
format.js {render 'layouts/sign_in.js.erb'}
end
end
Edit : here's my controller
respond_to do |format|
format.js {render 'devise/sessions/new.html.erb'}
end
In devise/sessions/new.html.erb, I put
<div> test</div>
<%= render "layouts/sign_in.js.erb" %>
and in layouts/sign_in.js.erb, I put console.log('test');
You can put renders inside the partial and segment your files one more level:
def ajax_call
respond_to do |format|
...
format.js {render 'layouts/grouped_sign_in.js.erb'}
end
end
# in _grouped_sign_in.js.erb
<%= render 'layouts/sign_in.js.erb' %>
<%= render 'layouts/create_account.js.erb' %>
What you can do is the following
In your controller
#template = ActionView::Base.new('app/views/static', {}, ActionController::Base.new).render(file: 'new').to_s
respond_to do |format|
format.js {render 'layouts/sign_in.js.erb'}
end
and then get your #template variable back in your sign_in.js.erb, <%= escape_javascript(#template) %>

How do I include HTML in a JS Rails response?

I have a FooController that responds to HTML and JS (AJAX) queries:
# app/controllers/foo_controller.rb:
class FooController < ApplicationController
layout 'foo'
def bar
respond_to do |format|
format.html # foo/bar.html.erb
format.js # foo/bar.js.erb
end
end
end
The templates to support it:
# app/views/layouts/foo.html.erb:
<html>...<%= yield %>...</html>
# app/views/layouts/foo.json.erb:
<%= yield %>
And an AJAX template in which I want to render a partial:
# app/views/foo/bar.js.erb:
dojo.byID('some_div').innerHTML = "<%= escape_javascript(render(:partial => 'some/partial')) %>";
If the JS template just has plain old JS in it (like alert('hi');), it uses my JS template. When I put in the render(:partial), though, it makes the whole response use the HTML template, which means it's no longer valid JS.
A possible solution is to use a function for the layout:
class FooController < ApplicationController
layout :choose_layout
...
private
def choose_layout
return nil if request.xhr?
'foo'
end
end
But my version should work! Why doesn't it?
The most recent Railscast covers this topic (using jQuery).
I'm not quite seeing where you might be going wrong, but here's a snippit from the Railscast that works just fine to render a partial:
// views/reviews/create.js.erb
$("#new_review").before('<div id="flash_notice"><%= escape_javascript(flash.delete(:notice)) %></div>');
$("#reviews_count").html("<%= pluralize(#review.product.reviews.count, 'Review') %>");
$("#reviews").append("<%= escape_javascript(render(:partial => #review)) %>");
$("#new_review")[0].reset();
Where are you storing your Javascript? Do you have an Application.js that you're keeping things in? If so, are you including "dojo" before "application" in your javascript_include_tag?
Try the following;
class FooController < ApplicationController
layout 'foo'
def bar
respond_to do |format|
format.html
format.js { render :layout => false }
end
end
end
Hope that helps.
J.K.

Categories