是否可以使用一个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有来自id = 1的用户的3个项目,如何在ajax成功回调中访问这些数据?
答案 0 :(得分:0)
我已经解决了使用:
render :json => { :data => @stores, :stores => render_to_string(:partial => "stores") }
目前,我已@stores数据并存储部分:)