来看看RJS的应用
我们首先要加上*.js文件,如修改application.rhtml:
[code]
<%= javascript_include_tag :defaults %>
[/code]
然后给Review的create方法加上js格式的应答:
[code]
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
[/code]
我们在创建Review的页面中使用form_remote_for:
[code]
<!-- products/show.rhtml -->
<% form_remote_for :review, :url => reviews_path, :html => { :id => 'review_form' } do |f| %>
...
<% end %>
[/code]
最后就是写create.rjs文件了:
[code]
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
[/code]
没多大新意,学习RJS看<<RJS Templates for Rails>>即可
我们首先要加上*.js文件,如修改application.rhtml:
[code]
<%= javascript_include_tag :defaults %>
[/code]
然后给Review的create方法加上js格式的应答:
[code]
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
[/code]
我们在创建Review的页面中使用form_remote_for:
[code]
<!-- products/show.rhtml -->
<% form_remote_for :review, :url => reviews_path, :html => { :id => 'review_form' } do |f| %>
...
<% end %>
[/code]
最后就是写create.rjs文件了:
[code]
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
[/code]
没多大新意,学习RJS看<<RJS Templates for Rails>>即可