現在簡単な、CRUD処理のアプリを作成し、練習をしています。
bootstrapを適用させるところまではできました。
そこでジャンボトロンを使って画像を表示させたくググって調べているのですが、解決できずかなり時間が経過してしまいました。
imdex.html.erbか、asset~で保管?するのような情報も目にしましたが、画像イメージはどこで編集してあげればいいかがわからないです。
"プリコンパイル"は"アセットパイプライン"を使っています。
一緒にみてもらえないでしょうか。
以下が現在の出力結果とエディタの内容です。
説明](88b095505b48284b37716cc507ade102.png)
index.html.erb
<div class="jumbotron jumbotron-fluid"> <div class="container"> <h1>書籍一覧</h1> <%= link_to '新規登録',new_book_path, class: "btn-sm btn-primary" %> </div> </div> <table class="table table-striped"> <thead> <tr class="table-info"> <th scope="col">登録されたid番号</th> <th scope="col">タイトル</th> <th scope="col">テキスト</th> <th scope="col">詳細</th> <th scope="col">編集</th> <th scope="col">削除</th> <th colspan="3"></th> </tr> </thead> <tbody> <% @books.each do |book| %> <tr> <td><%= link_to book.id, book_path(book) %></td> <td><%= book.title %></td> <td><%= book.text %></td> <td><%= link_to '表示',book_path(book), class: "btn-sm btn-success" %></td> <td><%= link_to '編集',edit_book_path(book), class: "btn-sm btn-secondary" %></td> <td><%= link_to '削除',book_path(book), method: :delete, class: "btn-sm btn-danger", data: {confirm: '本当に削除してもいいですか?'} %> </td> </tr> <% end %> </tbody> </table>
Gemfile
source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.5.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.2' # Use sqlite3 as the database for Active Record gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'mini_racer', platforms: :ruby # 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 ActiveStorage variant # gem 'mini_magick', '~> 4.8' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false 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 interactive console on exception pages or by calling '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 group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of chromedriver to run system tests with Chrome gem 'chromedriver-helper' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'bootstrap', '~> 4.2.1' gem 'jquery-rails'
application.scss
/* * This is a manifest file that'll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's * vendor/assets/stylesheets directory can be referenced here using a relative path. * * You're free to add application-wide styles to this file and they'll appear at the bottom of the * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * *= require_tree . *= require_self */ @import "bootstrap";
books.scss
// Place all the styles related to the books controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/
welcome.scss
// Place all the styles related to the welcome controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/
imageに追加した内容,jpg
application.scss
/* * This is a manifest file that'll be compiled into application.css, which will include all the files * listed below. * * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's * vendor/assets/stylesheets directory can be referenced here using a relative path. * * You're free to add application-wide styles to this file and they'll appear at the bottom of the * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * *= require_tree . *= require_self */ @import "bootstrap"; .jumbotron { background-image: image-url("栄養画像参考.jpg"); background-size: cover; }
サーバーで表示されたメッセージ
eiyou.jpgに変更後
route.rb
Rails.application.routes.draw do get 'welcome/index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :books root 'welcome#index' end
books_controller.rb
class BooksController < ApplicationController#to do index アクションを作る def index @books = Book.all end def show @book = Book.find(params[:id]) end def new @book = Book.new end def edit @book = Book.find(params[:id]) end def create @book = Book.new(book_params) if @book.save redirect_to @book else render :new end end def update @book = Book.find(params[:id]) if @book.update(book_params) redirect_to @book else render :edit end end def destroy @book = Book.find(params[:id]) @book.destroy! redirect_to books_path end private def book_params params.require(:book).permit(:title, :text) end end