実現したいこと
- コントローラーで、POST の場合の処理と、 GET の場合の処理を変更したい。
このため、
Ruby
1 if request.post? then 2 ・・・ 3 else 4 ・・・ 5 end
と記述しているが、必ず false (GET)と判定されてしまっている。
前提
書籍を参考に、入門用のWebサイトを作成しているが、
上述のような問題が発生してしまっている。
発生している問題
実行して、Webページより、アクセスし、Formから送信(POST)を行っているところまで、
確認しているが、その後、コントローラーで、request.post? が true とならない。
該当のソースコード
■コントローラ
Ruby
1 if request.post? then 2 @title = 'Result' 3 @msg = 'you typed: ' + params['input1'] + '.' 4 @value = params['input1'] 5 else 6 @title = 'Index' 7 ・・・ 8 end
■routes
Ruby
1Rails.application.routes.draw do 2 # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html 3 get 'hello/index' 4 get 'hello', to: 'hello#index' 5 post 'hello/index' 6 post 'hello', to: 'hello#index' 7end
■index.html.erb
Ruby
1<h1 class="display-4"><%= @title %></h1> 2<p><%= @msg %></p> 3<form method="POST" action="/hello/index"> 4 <input type="text" class="form-control" 5 name="input1" value="<%= @value %>"> 6 <input type="submit" class="btn btn-primary"> 7</form>
試したこと
Edge や Chrome で、試験してみたが、
ブラウザによる動作ではない模様。
補足情報(FW/ツールのバージョンなど)
Rails version: 7.0.4.2
Ruby version: ruby 3.1.3p185
環境:Windows 11
回答1件
あなたの回答
tips
プレビュー