Skip to content

Instantly share code, notes, and snippets.

@zhouxiaozhen-github
Created April 23, 2019 01:51
Show Gist options
  • Save zhouxiaozhen-github/7ffade491549adec536373f608266553 to your computer and use it in GitHub Desktop.
Save zhouxiaozhen-github/7ffade491549adec536373f608266553 to your computer and use it in GitHub Desktop.
rails项目使用AJAX的无脑四步骤
1 view 页面显示
<button id="floor_price_<%=product.id%>" onclick="get_floor_price(<%=product.id%>)">获取最低价</button>
2 view页面添加 ajax
<script>
function get_floor_price(p_id){
url = "/admin/products/floor_price/" + p_id
$.ajax({
url: url,
success: function(data){
$('#floor_price_' + p_id).text(data["floor_price"])
}
})
}
</script>
3 建立routes.rb
get '/admin/products/floor_price/:id', to: 'admin/products#floor_price'
4 controller 返回
def floor_price
product = Product.find_by_id(params[:id])
if product.nil?
@product = Product.page(1).per(18)
render :index
end
floor_price = product.floor_price
floor_price = "暂无" iffloor_price.blank?
render :json => { floor_price: floor_price }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment