質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

Q&A

解決済

1回答

2159閲覧

(Ruby on Rails) Bootstrapが上手く動かない

dounatsu

総合スコア78

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Bootstrap

BootstrapはウェブサイトデザインやUIのWebアプリケーションを素早く 作成する可能なCSSフレームワークです。 Twitter風のデザインを作成することができます。

0グッド

4クリップ

投稿2018/10/10 02:38

RailsでBootstrapを導入しようとしているのですが、上手く動きません。
色々試したのですが、エラーが出たり、読み込むクラスと読み込まないクラスがあったり、またサイトによってjqueryを入れたり、入れなかったり、application.scssに@import "bootstrap-sprockets";を入れたり入れなかったりしていて、色々なパターンで試したのですが、上手く動きません。(なぜかclass="btn btn-primary"は反映されるのにclass="navbar-default"は反映されないということが起きたりしました。)
混乱してよくわからなくなってしまったので、アドバイス頂けると幸いです。
よろしくお願いします。

環境
Mac OS high sierra 10.13.6
ruby 2.5.1
rails 5.1.6

htm

1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"> 5 <title>AwesomeEvents</title> 6 <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> 7 <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 8 <%= csrf_meta_tags %> 9 </head> 10 <body> 11 <header class="navbar navbar-default"> 12 <button class="btn btn-primary" type="button" name="button">button</button> 13 </body> 14</html>
#gemfile source 'https://rubygems.org' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo_name}.git" end # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.6' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets gem 'sass-rails' gem 'bootstrap', '~> 4.1.1' gem 'bootstrap-sass' gem 'jquery-rails' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby gem 'slim-rails' gem 'html2slim' gem 'omniauth' gem 'omniauth-twitter' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.2' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

scss

1#application.scss 2@import "bootstrap"; 3 4/* 5 * This is a manifest file that'll be compiled into application.css, which will include all the files 6 * listed below. 7 * 8 * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's 9 * vendor/assets/stylesheets directory can be referenced here using a relative path. 10 * 11 * You're free to add application-wide styles to this file and they'll appear at the bottom of the 12 * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS 13 * files in this directory. Styles in this file should be added after the last require_* statement. 14 * It is generally better to create a new file per style scope. 15 * 16 17 */ 18

javascript

1#application.js 2 3 4// This is a manifest file that'll be compiled into application.js, which will include all the files 5// listed below. 6// 7// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's 8// vendor/assets/javascripts directory can be referenced here using a relative path. 9// 10// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 11// compiled file. JavaScript code in this file should be added after the last require_* statement. 12// 13// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details 14// about supported directives. 15// 16// 17//= require rails-ujs 18//= require jquery 19//= require bootstrap-sprockets 20//= require turbolinks 21//= require_tree . 22 23

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takumiabe

2018/10/10 10:59

"bootstrap-sass"は、Boostrap v2.x - v3.x "bootstrap"は Bootstrap v4 向けのようです。意図としてはどちらを使おうとしているのでしょうか?
dounatsu

2018/10/10 14:55

そうなんですね。知らなかったです。。。意図としてはBootstrap v4を使おうと思っています。よろしくお願いします。
dounatsu

2018/10/10 19:07

今確認したらnavbar-defaultはv4では使われていないのですね。他の物も意図した物とは違ったりしているので、ちょっと確認してみます。
guest

回答1

0

ベストアンサー

1 Bootstrapのバージョン

Bootstrap v4を使いたいとのことなので、
v2〜v3のモノである、gem "bootstrap-sass"は削除しましょう。

bootstrapのREADMEbootstrap-sassのREADMEを見比べて、
bootstrap-sass用の初期化処理は削除しましょう。
(bootstrap-sprocketsあたりが該当するようです)

他にも、セットアップ手順が示されているので、見落としのないようにしましょう。

2 jQueryについて

Rails5においては、jQueryがデフォルトでは導入されなくなりました。
(jquery-ujsがrails-ujsに置き換えられています。)
なので、導入していなければ、'jquery-rails'などを自分で導入する必要があります。
(設定等も含め)

3 Bootstrapのクラスについて

Bootstrapはバージョンによってあったりなかったりするclassが結構あるので、
基本的には、自分が導入したバージョンをしっかり把握した上で、

公式のドキュメントの右上のバージョンから、自分が使っているバージョンを選んだ上で、用途に応じてそのバージョンでの書き方を探す、というのがおすすめです。

navbar-defaultば、v3には存在しますが、v4ではなくなっていますね。

投稿2018/10/11 01:25

takumiabe

総合スコア661

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dounatsu

2018/10/12 06:19

色々調べた結果上手く動きました。 貴重なアドバイスありがとうございました。 忘備録的な意味を込めてv3からv4へ移行するための方法をまとめた公式のドキュメントのリンクを貼って起きます。 https://getbootstrap.com/docs/4.0/migration/
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問