0
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

RailsのViewに複数のsubmitボタンを設置して押されたボタンの値を取得する

やりたいこと

以下のように、複数のボタンを設置して、押されたボタンの値を取得します

View

selectコントローラの、purposeアクションに飛びます

button.html.erb
<%= form_tag({ :controller => 'select', :action => 'purpose' }) do %>
        <%= submit_tag "A" %><br> 
        <%= submit_tag "B" %><br>
        <%= submit_tag "C" %><br>
        <%= submit_tag "D" %><br>       
<% end %>

Controller

params[:commit]にボタンの値が入ります

select_controller.rb
class SelectController < ApplicationController   
    def purpose
        #A,B,C,Dなど、ボタンの値が入る
        @name=params[:commit]        
    end
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?