是否可以仅使用一个 ajax 请求来呈现部分和检索到的模型数据?
# app/models/store.rb
def index
user = current_user;
@stores = Store.where(:user_id => user.id)
render :partial => "stores", :layout => false
end
# app/views/stores/_stores.html.erb
<div class="stores">
<select id="stores" class="dropdown">
<% @stores.each do |store| %>
<option value="<%= store.id %>"><%= store.name %></option>
<% end %>
</select>
</div>
# app/views/users/index.html.erb
$.ajax({
type : "GET",
url : "/stores",
success: function(data) {
$("div").html(data); //rendering partial
// how to get @stores variable??
}
});
想象@stores 有 3 个来自 id = 1 的用户的项目,我如何在 ajax 成功回调中访问这些数据?