原创作者: hideto   阅读:1669次   评论:0条   更新时间:2011-05-26    
来看看RJS的应用

我们首先要加上*.js文件,如修改application.rhtml:
<%= javascript_include_tag :defaults %>

然后给Review的create方法加上js格式的应答:
def create
  @review = Review.create!(params[:review])
  flash[:notice] = "Thank you for reviewing this product"
  respond_to do |format|
    format.html { redirect_to product_path(@review.product_id) }
    format.js
  end
end

我们在创建Review的页面中使用form_remote_for:
<!-- products/show.rhtml -->
<% form_remote_for :review, :url => reviews_path, :html => { :id => 'review_form' } do |f| %>
...
<% end %>

最后就是写create.rjs文件了:
page.insert_html :bottom, :reviews, :partial => 'review', : object => @review
page.replace_html :reviews_count, pluralize(@review.product,reviews.size, 'Review')
page[:review_form].reset
page.replace_html :notice, flash[:notice]
flash.discard

没多大新意,学习RJS看<<RJS Templates for Rails>>即可
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics