2014-07-05 36 views
1

我很新,很怕jquery。我很快就会克服它,但是我想要实现的是,在表单提交后,会弹出一个提示,告诉它是成功还是失败。我有这个工作,但它是丑陋的警报弹出窗口。我想要的是靴子警报通知或警报警告。ajax表单提交rails使用引导后回调

我控制器目前正在寻找这样

def create 
    @subscription = Subscription.new(subscription_params) 

    respond_to do |format| 
     if @subscription.save 
     format.html 

     format.js { render :js=>'alert("You have been added to the mailing list");' } 
     else 
     format.html 
     format.js { render :js=>'alert("You have entered an invalid email address");' } 
     end 
    end 
    end 

我已经bootstrap.js装,我只是不知道该如何实现它。

+1

你应该使用jquery'$('#myModal')。modal(options);'参考bootstrap [documentation](http://getbootstrap.com/javascript/#modals) –

回答

1

创建您的模式,假设它有一个ID“myModalSuccess”如果发生故障,则不需要模块,只需渲染表单并显示错误。你的控制器看起来像:

def create 
    @subscription = Subscription.new(subscription_params) 

    respond_to do |format| 
    if @subscription.save 
     format.html{redirect_to your_path} 
     format.js{} 
    else 
     format.html{render action: 'new'} 
    end 
    end 
end 

现在,你可以把它叫做你的create.js.erb文件里面一样:

$("#myModalSuccess").modal("show"); 

您也可以通过其他的选择模式方法中,对于细节看看bootstraps javascript documentation

+1

打开模式如果瓦利实验失败,我该如何渲染错误? –

+0

@rico_mac因为我认为你应该简单地呈现你的新动作。我不认为你需要你的模式来防止失败,因为你的表单无论如何都会显示错误。更正我的答案以及 – Mandeep

+0

是的,我知道你的意思。这只是我有一个这样的表格,所以我不认为我能够<%= bootstrap_form_for Subscription.new,layout :: inline,url:'/ subscriptions',html:{remote:true} do | f | %> \t \t <%= f.text_field:电子邮件,hide_label:真%> \t \t <%= f.submit '加入邮件列表' %> \t \t <% end %> –