2014-07-05 35 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 %> –