2013-10-21 56 views
0

如何在同一個表單上有多個submit_tag按鈕? 現在我得到它只適用於一個按鈕,但我不知道如何讓form_tag處理多個路徑。Ruby on rails:同一表單上的多個submit_tag

的routes.rb

resources :actions do 
    end 
    root 'home#start' 
    match '/home/add', to: 'home#add', via: 'get' 
    match '/home/subtract', to: 'home#subtract', via: 'get' 
    match '/home/multiply', to: 'home#multiply', via: 'get' 

Start.html.erb

<%= form_tag "/home/add",:method => "get" do %> 
<p></p> 
<p> 
<%= label_tag :entered, "Please enter value:" %> </br> 
<%= text_field_tag :entered %> 
</p> 
<p></p> 
<p> 
<%= label_tag :entered2, "Please enter value:" %> </br> 
<%= text_field_tag :entered2 %> 
</p> 
<%= submit_tag "add", :controller => "home", :action => "add" %> 
<%= submit_tag "subtract", :controller => "home", :action => "subtract" %> 
<%= submit_tag "multiply", :controller => "home", :action => "multiply"%> 
<% end %> 

請指教。 預先感謝您。

+0

你必須通過JavaScript做 – userxyz

回答

2

我不知道你是否可以讓它走向不同的道路。但是會有這樣的幫助嗎?

只需要一個動作,並根據點擊的提交按鈕在控制器中執行一些操作。你的路線看起來像

*Routes.rb* 
    resources :actions do 
    end 
    root 'home#start' 
    match '/home/operation', to: 'home#add', via: 'get' 

您查看會

<%= form_tag "/home/operation",:method => "get" do %> 
<p></p> 
<p> 
<%= label_tag :entered, "Please enter value:" %> </br> 
<%= text_field_tag :entered %> 
</p> 
<p></p> 
<p> 
<%= label_tag :entered2, "Please enter value:" %> </br> 
<%= text_field_tag :entered2 %> 
</p> 
<%= submit_tag "Add"%> 
<%= submit_tag "Subtract"%> 
<%= submit_tag "Multiply"%> 
<% end %> 

在你的控制器

class HomeController < ApplicationController 
    def operation 
    send(params[:commit].downcase) #params[:commit] will have one of the values "Add", "Subtract", "Multiply" 
    end 

    private 
    def add 
    #do something 
    end 

    def subtract 
    #do something 
    end 

    def multiple 
    #do something 
    end 
end 
相關問題