前提・実現したいこと
Rails 4にて以下の要領でお問い合わせフォームを作成中です。
https://www.imd-net.com/column/1731/
発生している問題・エラーメッセージ
問い合わせ機能そのものではなく、jqueryが機能しない問題です。
使っているbootstrapのテンプレートの、ドロップダウンが機能しません。
以下ソースコードの通り、ホームと問い合わせページのみのシンプルな構造です。
turbolinksはアプリケーション全体で切ってあります。
該当のソースコード
route.rb
Rails.application.routes.draw do get 'contact' => 'inquiry#contact' post 'contact/confirm' => 'inquiry#confirm' post 'contact/thanks' => 'inquiry#thanks' root 'static_pages#home' match '/home', to: 'static_pages#home', via: 'get' end
inquiry_controller.rb
class InquiryController < ApplicationController def contact # 入力画面を表示 @inquiry = Inquiry.new render :action => 'contact' end def confirm # 入力値のチェック @inquiry = Inquiry.new(params[:inquiry]) if @inquiry.valid? # OK。確認画面を表示 render :action => 'confirm' else # NG。入力画面を再表示 render :action => 'index' end end def thanks # メール送信 @inquiry = Inquiry.new(params[:inquiry]) InquiryMailer.received_email(@inquiry).deliver # 完了画面を表示 render :action => 'thanks' end end
gemfile
source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.6' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.1.0' # See https://github.com/rails/execjs#readme for more supported runtimes gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks # gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # gem 'jquery-turbolinks' gem 'figaro' gem 'passenger' gem 'jquery-ui-rails' group :production do gem 'pg' gem 'rails_12factor' end group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' gem 'pry-rails' end group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end
application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files // listed below. // // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. // // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the // compiled file. // // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // //= require jquery //= require jquery_ujs //= require_tree .
試したこと
もともとホームでもjqueryの発火に問題があり、turbolinksを切ったら機能するようになりましたが、contactページでは変わらず、いろいろと調べたところ、以下の情報に行き着きました。
引用元 https://railsguides.jp/action_mailer_basics.html
"GET以外のリンクが機能するにはrails-ujsまたはjQuery UJSが必須です。また、これらはメイラーテンプレートでは機能しません(通常のGETリクエストが出力されます)。"
ソースコードの通り、rails-ujsはおそらく入っているはずです。
どなたか、ヒントをご教示いただければと思います。
よろしくお願いいたします。
あなたの回答
tips
プレビュー