2013-03-25 26 views
1

我有我的表單標籤,我的表單中也有兩個按鈕。如何在點擊提交按鈕時在我的表單中調用不同的操作。如何在兩個提交標籤中提及表單中的不同動作?

這裏是我的代碼:

<%= form_for :attachment_metadata, :url=>{:action=>'delete_files'}, :html=>{:onsubmit=> "return confirm('Are you sure, you want to delete selected files?');",:multipart => true} do |f| %> 
    <table> 
    ..........Some stuff here.......... 
    </table> 
<%= submit_tag 'Reprocess', :class =>'button' %> 
<%= submit_tag 'Remove', :class =>'button' %> 
<% end %> 

論「再加工」我想打電話給不同的動作,但在相同的形式點擊。

我怎樣才能做到這一點?

請幫

在此先感謝

+1

看到http://stackoverflow.com/questions/13987512/how-can-i-get-f-submits-name-parameter-in-controller/13988021#13988021 – shweta

回答

1

params[:commit]可以區分兩個提交標籤的動作。
更新時間:

@Action =參數[:提交]

它給@action值爲 「Reprocess」 如果你點擊Reprocess按鈕,並給出了 「Remove」 值,如果你點擊Remove按鈕,

+0

我該如何使用它? –

+0

您可以在提交操作中使用它。您可以通過使用params [:commit]區分哪個按鈕被點擊,它會給出您點擊的butten的值,只需檢查 –

+0

查看我的更新回答 –

1

這是覆蓋在Railscast 38。 M確定它會解決你的問題。它非常簡單。每當你提交表單時,它會發送一些關於你已經執行的提交的參數,我建議你看看railscast,你會完全理解。

+0

感謝它可能有助於 –

+0

我想要提交重新處理按鈕 –

+0

m提交重新處理操作確定您可以撥打電話,只需看看railscast。 –

0

你可以有兩種不同的形式,你可以使用jQuery提交你的表單。在這種情況下,您可以將表單提交到特定位置。

<form id="target" action="destination.html"> 
    <input type="text" value="Hello there" /> 
    <input type="submit" value="Go" /> 
</form> 
<div id="other"> 
    Trigger the handler 
</div> 

$('#target').submit(function() { 
    alert('Handler for .submit() called.'); 
    return false; 
}); 
+0

這是與jQuery很好的解決方法。 –

相關問題