loading rails partial via ajax - javascript

I'm trying to load a partial via ajax. When I try to render the partial all I get is the render code printed to the screen.
JS
$("#filter_menu2 input:checkbox").click ->
$.get "search", (data) ->
$("#products").html("<%= escape_javascript(render(:partial=>'new_shop/shop_search_results')).html_safe %>")
return
controller
respond_to do |format|
format.html #search.html.erb
format.js
end
This is just getting printed the screen
<%= escape_javascript(render(:partial=>'new_shop/shop_search_results')).html_safe %>

You have to put your code in some search.js.erb and put respond_to :js at the top of your controller and at last return format.js {render "search.js.erb"}.
So, now search.js.erb contains this line
$("#products").html("<%= escape_javascript(render(:partial=>'new_shop/shop_search_results')).html_safe %>")

Related

respond_to do |format| gives "UnknownFormat in UsersController#show" in rails 5 app

In my rails 5 app I want to render partials in a div in the views/users/show.html.erb on link click.
I believe I have set things up accordingly but rails gives me this error ActionController::UnknownFormat in UsersController#show
ActionController::UnknownFormat
respond_to do |format|
format.js { render :show_kwst }
end
I'm kind of lost here, I've done similar things in rail 4 by using bootstrap tabs with out any problems.
I've also added the 'responders' gem to the gemfile
This is my views/users/show.html.erb
<%= link_to 'Kwst', user_path%>
<div id="content"></div>
This is my users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
#user_posts = #user.posts
#user_kwsts = #user.kwsts
respond_to do |format|
format.js { render :show_kwst }
end
end
end
And here is the `views/users/show_kwst.js.erb``
$("#content").html("<%=escape_javascript(render :partial=>"shared/show_kwst")%>");
Am I missing something?
Try to the following order, you can follow this Rails Guide About format.js
respond_to do |format|
format.html { #user_kwsts }
format.js
end
Notice the format.js in the respond_to block; that allows the controller to respond to your Ajax request. You then have a corresponding app/views/users/show_kwst.js.erb view file that generates the actual JavaScript code that will be sent and executed on the client side.

JS is not executing rails

I try to display a view in a modal but i dont know why my JS is not executing, i saw similar questions about it but it doesnt solve my issue,
My link
def create
document = Document.find(params[:document_id])
order = Order.create!(doc_sku: document.doc_sku, amount: document.price, state: 'pending')
redirect_to new_order_payment_path(order, format: :js, remote: true)
end
controller:
def new
#document = Document.last
respond_to do |format|
format.js {render layout: false, content_type: 'text/javascript'}
end
end
new.js.erb
$("#newModal .modal-content").html('<%= j render "payments/form" %>');
$("#newModal").modal();
I test it with a simple alert but nothing change
In my log
Processing by PaymentsController#new as JS
Parameters: {"remote"=>"true", "order_id"=>"35"}
I dont have warnings/errors in the network or console of my browser
Content type is wrong. Change render to just.
respond_to do |format|
format.js
end
Should work.

Rails respond_to js on rails controller method

I'm trying to run a js.erb file for when a worker has been updated, but nothing happens. as if it's not running the js.erb file
I have this in my controller:
def update
#worker = Worker.find(params[:id])
respond_to do |format|
if #worker.update(worker_params)
format.js
format.html { redirect_to #worker }
else
format.html {render :edit }
end
end
end
and a corresponding file at:
/app/views/workers/update.js.erb
contents of that file is a publish helper from private_pub gem
<% publish_to "/messages/new" do %>
$("#present").load(location.href + " #present");
<% end %>
I don't get any errors or anything that I can see. can't quite figure out what I'm missing
UPDATE:
I added remote: true to the update form, now the js file is being executed as expected. but the issue now is that when I press submit, he runs the js file but stops after that, he is supoused to redirect the page back to the workers show again.

Rails - adding ajax to partials, 'undefined method `render''

I have a form partial, within a div with id "chapcomments", with a submit tag -
<%= f.submit "Post", remote: true %>
In the correct view folder, I have create.js.erb -
$('#chapcomments').html("<%=j render 'shared/chap_comment_complete' %>");
And in the controller I have the format block which includes
def create
#chap_comment = current_user.chap_comments.build(chap_comment_params)
respond_to do |format|
if #chap_comment.save
format.js
format.html {redirect_to chapter_path(#chap_comment.chapter)}
else
format.html { render :new }
format.json { render json: #chap_comment.errors, status: :unprocessable_entity }
end
end
end
...but I'm not getting ajax behaviour...
Where am I going wrong?
You've made the change so that the JS is in the right js.erb file in the view, but you need to move the remote: true part from the submit tag into your form_for declaration, otherwise the format.html response block will be rendered instead of format.js.

How to make a respond_to by AJAX in Rails 3?

I have a form, that I send with using AJAX. The form is in the FirstsController. I need the form send to SecondsController. The form is sent and data saved. Then I wanna change the form by a text, I try to do by this way:
def create
...saving...
respond_to do |format|
format.js {
render :update do |page|; page << "$('#id_element').html('hellllloooooo');" end
}
format.html {}
end
end
And I ma getting this error:
ActionView::MissingTemplate (Missing template seconds/update, application/update with {:handlers=>[:erb, :builder, :coffee], :formats=>[:js, :html], :locale=>[:en, :en]}.):
How should I set the JS request back to the FirstsController? + is needed any help *.js file?
This is the right set up to do it:
def create
...saving...
respond_to do |format|
format.js {
render 'firsts/action_that_you_want'
}
format.html {}
end
end
And in the FirstsController is needed to have created the the file action_that_you_want.js.erb.

Categories

Resources