Reputation: 373
I have a model named User and in its index.html.erb file, has a link which calls an ajax request responsible for retrieving user's authenticated radars. There it is:
//user.js
function retrieve_radars() {
$.ajax({
type: "GET",
url: "/radars",
success: function(data) {
console.log(data);
}
});
}
#app/controllers/radars_controllers.rb
def index
user = current_user;
@radars = Radar.where(:user_id => user.id)
@radars
end
How could I iterate @radars instance variable in "users/index.html.erb", after the success ajax response? I would like to do something like this:
<% unless @radars.nil? %>
<table>
<% @radars.each do |radar| %>
<tr>
<td>Name</td>
<td><%= radar.name %></td>
</tr>
<% end %>
</table>
<% end %>
Any help? Thanks
Upvotes: 0
Views: 990
Reputation: 2825
//user.js
function retrieve_radars() {
$.ajax({
type: "GET",
url: "/radars",
success: function(data) {
//load the response from radar#index into the div (or any other element
$("#my_div").html(data);
}
});
}
#app/controllers/radars_controllers.rb
def index
user = current_user;
@radars = Radar.where(:user_id => user.id)
render :partial => "radars", :layout => false #render partial.
end
#app/views/radars/_radars.html.erb create partial file
<% unless @radars.nil? %>
<table>
<% @radars.each do |radar| %>
<tr>
<td>Name</td>
<td><%= radar.name %></td>
</tr>
<% end %>
</table>
<% end %>
Upvotes: 2