How to Render the Ajax Response in Rails

How to render the AJAX response in RAILS

In your respond_to block put format.js above format.html.

Make sure you have index.js.erb in your view folder for the controller.
This index.js.erb(comes from the action name) should have a jQuery statement like the following if you're using rails 3.1+:

$('#DOMelementToPlaceContent').html(<%= escape_javascript(render :partial => "partial/location") %>);

This will replace the content of the DOM element with ID DOMelementToPlaceContent with the content in the specified partial.

Also, you should think about moving the logic for your search to a search action in the same controller in which case you'll need a search.js.erb file in the view folder for the controller.

How to render rails partial after a successful ajax POST request?

You need to set your controller up to respond to JS. Remember that js != json.

def create
@run = Run.new(run_params)
@runs = Run.all
respond_to do |format|
if @run.save
format.json { render json: @runs }
format.html { redirect_to :back, :notice => 'Run was successfully created.' }
format.js
else
format.html { render action: "new" }
format.json { render json: @run.errors, :status => :unprocessable_entity }
format.js { render action: "new" }
end
end
end

Not passing a block will trigger the rails default behaviour of rendering create.erb.js.

Do I need an .ajax method if I have a create.js.erb file?

No. You're using the Rails UJS driver to send an ajax request already. Otherwise the response type in the log would be :html.

Other SO questions mention setting the dataType = "script". To the best of my knowledge, I should not need this file.

You're correct. Rails UJS defaults to using a javascript accept type header. Its only if you want a JSON response for example that you need to set the dataType.

How to render controller variables in view using Ajax/Ruby on Rails?

Rails variables @data_count and @gold_data_count in console.log returns their value when page was loaded, but not when ajax is success.

Your controller can render json with partial, if you want

respond_to do |format|
format.js { render json: { html: render_to_string(partial: 'data_stats', locals: { data_count: @data_count, gold_data_count: @gold_data_count }) } }
end

Partial with stats _data_stats.html.erb:

<div id="data_stats">
<label class="col-sm-2 control-label">Data count</label>
<span id="data_count"><%= @data_count %></span>
<label class="col-sm-2 control-label">Gold Data count</label>
<span id="gold_data_count"><%= @gold_data_count %></span>
</div>

And use it in your js, such as

$.ajax({
type: "GET",
url: "<%= admin_data_stats_path%>",
data: Object.keys(data),
dataType: 'json'
}).done((response) => {
$('#data_stats').replaceWith(response.html)
})

My Ajax call returns js.erb file as the response in the browser. How do I make it return json contents, but also execute the js.erb file?

How can I show json response in browser while js.erb file executes?
Thank you :)

If I understood correctly what you are asking, the answer is you can't. Your response's Content-Type will be either an application/json or an application/javascript.

The browser will use this header to handle the response.

Ajax on Rails How to use Ajax append method for render HTML?

A few things you should know and will have to learn as you go on:

Rails links come with a built in function to send ajax requests like this:

<%= link_to "My ajax request text", post_new(@post), remote: true %>

The "remote: true" option sends an ajax request for that link for you to the specified path (in this case a new post).

Your controller receives every request and responds by redirecting or rendering views which refreshes the page. But when you do an ajax request you don't want that. What you want is that your controller responds via Ajax too. Once again, Rails has a built in function to do that:

def billpreview
# write all your logic and set instance variables for the objects you want
# @salesmenu
respond_to do |format|
format.html
format.js
end
end

respond_to looks at your request and determines if it is an HTML request (no Ajax) or an Ajax request. If it is an Ajax request it responds with a Javascript file, else it responds with an HTML file which is what it does by default.

In your view folder you will need to create a new file now for your javascript file which Rails can send when you do an Ajax request:

billpreview.html.erb
billpreview.js.erb

billpreview.html.erb is what you have been using until now. billpreview.js.erb is what you will use when you request an Ajax response.

Inside of billpreview.js.erb you can use the same instance variables you used in billpreview.html.erb.

#billpreview.js.erb
$('#id_in_your_view').append('<%= j render @salesmenu %>');

Usually you will pick an id in your view ($('#id_in_your_view')) to which you can append or prepend your content. You will have to use <%= j render @salesmenu %> not just "render" because you are using javascript. Once you understand the logic behind it, it becomes really easy. Good luck.

I recommend you watch these 2 youtube videos:

https://www.youtube.com/watch?v=FBxVN7U1Qsk

https://www.youtube.com/watch?v=2Il7PPhen3o



Related Topics



Leave a reply



Submit