1

当我使用 :remote => true formtastic 在成功提交后不会自动清除表单

此外,如果我使用远程表单来更新数据,如下面的代码所示,该表单始终与该模型相关联,不允许我保存新模型,即使我尝试使用 build 和尝试创建新模型。在 js 中通过本地人传递 ti

所以这里是formtastic视图

<%= semantic_form_for @deliverable,:remote=>true do |f| %>
<%= f.input :quantity, :as => :number , :name => "items"%>
<%= f.input :product_id, :as => :select, :collection => current_user.products.all %>
<%= f.actions%>
<% end %>

这是控制器

def create
@micropost = current_user.microposts.find(Rails.cache.read('mp_id'))
@deliverable = @micropost.deliverables.build(params[:deliverable])
@curr_product = current_user.products.find(params[:deliverable][:product_id])
@deliverable.del_price_cents = @deliverable.quantity * @curr_product.price_cents
if @deliverable.save
   @del_items = @micropost.deliverables.find(:all)

  respond_to do |format|
    format.html {redirect_to root_path}
    format.js
 end   
else
  respond_to do |format|
    format.html {redirect_to root_path}
    format.js    {render :text => @deliverable.errors.full_messages.to_s}
 end   




end
end
def edit
 @deliverable = Deliverable.find(params[:id])
 respond_to do |format|

   format.js 
 end

 end
 def update
 @deliholder = Deliverable.find(params[:id])
 if @deliholder.update_attributes(params[:deliverable])
 @micropost = current_user.microposts.find(Rails.cache.read('mp_id'))

 @del_items = @micropost.deliverables.find(:all) 
 @deliverable = @micropost.deliverables.new


  respond_to do |format|
    format.js 
  end
  else
  respond_to do |format|
    format.html {redirect_to root_path}
    format.js    {render :text => @deliverable.errors.full_messages.to_s}
  end 
 end  
end

继承人的js视图进行编辑

$("#deliver-list").hide();
$("#deliver-holder").html("<%= escape_javascript( render(    'shared/deliverable_formtastic',:locals => {:deliverable => @deliverable }))%>");

和用于更新的 js

alert("the deliverable has been updated ")
$("#deliver-list").html("<%= escape_javascript( render(  'shared/deliverable_list' ,:locals => {:del_items => @del_items } ))%>");
$("#deliver-list").show();
$("#deliver-holder").empty();
$("#deliver-holder").html("<%= escape_javascript( render( 'shared/deliverable_formtastic',:locals => {:deliverable => @deliverable }))%>");

我尝试用@deliholder 替换@deliverable 进行更新,如上所示,没有区别,在js locals 中传递或不传递@deliverables 也没有区别

创建.js

alert("the deliverable has been saved to the  bill ");
$('#fc')[0].reset();
$("#deliver-list").html("<%= escape_javascript( render(  'shared/deliverable_list' ,:locals => {:del_items => @del_items } ))%>");
4

0 回答 0