Saturday, 7 September 2013

How to use rails instance variable to create partial and return model's data?

How to use rails instance variable to create partial and return model's data?

My rails app has an User model with has_many relation to Radar model. So,
when the user logs in, I have to show on user's index view all radars
created by him and add markers on google maps with the location of them. I
created a partial to render radar's view information, but, how can I load
the radars object if I have to return the partial too?
# app/models/radar.rb
class Radar < ActiveRecord::Base
attr_accessible :name, :radius, :latitude, :longitude, :user_id
end
# app/controllers/radars_controller.rb
def index
user = current_user;
@radars = Radar.where(:user_id => user.id)
render :partial => "radars", :layout => false
end
# app/views/radars/_radars.html.erb
<div class="radars">
<select id="radars" class="dropdown">
<% @radars.each do |radar| %>
<option value="<%= radar.id %>"><%= radar.name %></option>
<% end %>
</select>
</div>
# app/views/users/index.html.erb
#onLoad();
$.ajax({
type: "GET",
url: "/radars",
beforeSend: function(x) {
x.setRequestHeader('X-CSRF-Token',
$('meta[name="csrf-token"]').attr('content'))
},
success: function(data) {
#div to load _radars.html.erb partial info
$(".panel-left").html(data);
# how to get here radar.name, radar.latitude, radar.longitude?
}
});
Thanks!

No comments:

Post a Comment