Railsを使っておすすめの本を投稿できるアプリケーションを作成しています。
部分テンプレートの設定をしてサーバで確認してみたらSintaxErrorが出てしまいました。
2時間くらいここで止まってしまっているので解決方法を教えていただけると幸いです。
実現したいこと
SintaxErrorの解消
(Viewのbooks/index.html.erbとusers/show.html.erbの共通化)
発生している問題・エラーメッセージ
ActionView::SyntaxErrorInTemplate in UsersController#show Encountered a syntax error while rendering template: check <div> <h1>User info</h1> <h3>name <%= @user.name %></h3> <%= image_tag @user.get_profile_image(100,100) %> <% if @user.id == current_user.id %> <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> <% end %> <% end %> <h3>introduction</h3> </div> <%= render '/books/books',books: @books %> app/views/users/show.html.erb:14: syntax error, unexpected end, expecting end-of-input
該当のソースコード
Rails
1#views/users/show.html.erb 2<div> 3 <h1>User info</h1> 4 <h3>name <%= @user.name %></h3> 5 <%= image_tag @user.get_profile_image(100,100) %> 6 <% if @user.id == current_user.id %> 7 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 8 <% end %> 9 <% end %> 10 <h3>introduction</h3> 11</div> 12 13<%= render '/books/books', books: @books %>
試したこと
<% end %>
をerror文の前後で書き足したりしましたが、解決しませんでした。
補足情報(FW/ツールのバージョンなど)
Cloud9,EC2,t2.micro,Rails version: 6.1.6.1
Ruby version: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
app/views/_books
1###部分テンプレート 2<% books.each do |book| %> 3<div> 4 <%= link_to user_path(book.id) do %> 5 <P><%= image_tag book.user.get_profile_image(100,100) %></P> 6 <% end %> 7 <h3>Title</h><br> 8 <%= link_to book.title, book_path(book.id) %></p> 9 <h3>Opinion</h><br> 10 <%= book.body %> 11</div>
views/books/index.html.erb
1<%= render '/books/books', books: @books %> 2<div class="container"> 3<h2>Books</h2> 4 <div class="row"> 5 <div class="col-md-8" offse></div> 6 <table class="table table-striped"> 7 <thead> 8 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> 9 <tr> 10 <th></th> 11 <th>Title</th> 12 <th>Opinion</th> 13 <th colspan="3"></th> 14 </tr> 15 <% @books.each do |book| %> 16 <tr> 17 <td> 18 <%= link_to user_path(book.id) do %> 19 <% if book.profile_image.attached? %> 20 <%= image_tag User.get_profile_image(100,100) %> 21 <% else %> 22 <%= image_tag 'no_image', size: "100x100" %> 23 <% end %> 24 <% end %> 25 </td> 26 <td><%=link_to book.title, book_path(book.id) %></td> 27 <td><%= book.body %></td> 28 </tr> 29 <% end %> 30 </thead> 31</table> 32 </div> 33</div>
Usersコントローラ
1Rails.application.routes.draw do 2 get 'homes/about', to: 'homes#show', as: :about 3 post 'books/book.id' => 'books#create' 4 devise_for :users 5 root to: "homes#top" 6 resources :books, only: [:new, :show, :edit, :index, :create] 7 resources :users, only: [:show, :edit, :index] 8 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 9end
Booksコントローラ
1class BooksController < ApplicationController 2 def new 3 @book = Book.new 4 end 5 6 def create 7 @book = Book.new(book_params) 8 @book.user_id = current_user.id 9 @book.save 10 redirect_to book_path(@book.id) 11 end 12 13 def index 14 @books = Book.all 15 end 16 17 def show 18 end 19 20 def edit 21 end 22 23 private 24 25 def book_params 26 params.require(:book).permit(:title, :body, :user_id, :profile_image) 27 end 28 29end
Userモデル
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :books, dependent: :destroy 8 has_one_attached :profile_image 9 10 def get_profile_image(width, height)#ユーザーアイコン設定# 11 unless profile_image.attached? 12 file_path = Rails.root.join('app/assets/images/sample-author1.jpg') 13 profile_image.attach(io: File.open(file_path), filename: 'default-image.jpg', content_type: 'image/jpeg') 14 end 15 profile_image.variant(resize_to_limit: [width, height]).processed 16 end 17end
Bookモデル
1class Book < ApplicationRecord 2 belongs_to :user 3 has_one_attached :profile_image 4end
schema
1# This file is auto-generated from the current state of the database. Instead 2# of editing this file, please use the migrations feature of Active Record to 3# incrementally modify your database, and then regenerate this schema definition. 4# 5# This file is the source Rails uses to define your schema when running `bin/rails 6# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to 7# be faster and is potentially less error prone than running all of your 8# migrations from scratch. Old migrations may fail to apply correctly if those 9# migrations use external dependencies or application code. 10# 11# It's strongly recommended that you check this file into your version control system. 12 13ActiveRecord::Schema.define(version: 2022_07_25_122723) do 14 15 create_table "active_storage_attachments", force: :cascade do |t| 16 t.string "name", null: false 17 t.string "record_type", null: false 18 t.integer "record_id", null: false 19 t.integer "blob_id", null: false 20 t.datetime "created_at", null: false 21 t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" 22 t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true 23 end 24 25 create_table "active_storage_blobs", force: :cascade do |t| 26 t.string "key", null: false 27 t.string "filename", null: false 28 t.string "content_type" 29 t.text "metadata" 30 t.string "service_name", null: false 31 t.bigint "byte_size", null: false 32 t.string "checksum", null: false 33 t.datetime "created_at", null: false 34 t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true 35 end 36 37 create_table "active_storage_variant_records", force: :cascade do |t| 38 t.integer "blob_id", null: false 39 t.string "variation_digest", null: false 40 t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true 41 end 42 43 create_table "books", force: :cascade do |t| 44 t.text "tite" 45 t.text "body" 46 t.integer "user_id" 47 t.datetime "created_at", precision: 6, null: false 48 t.datetime "updated_at", precision: 6, null: false 49 t.text "title" 50 end 51 52 create_table "users", force: :cascade do |t| 53 t.string "email", default: "", null: false 54 t.string "encrypted_password", default: "", null: false 55 t.string "reset_password_token" 56 t.datetime "reset_password_sent_at" 57 t.datetime "remember_created_at" 58 t.string "name" 59 t.datetime "created_at", precision: 6, null: false 60 t.datetime "updated_at", precision: 6, null: false 61 t.index ["email"], name: "index_users_on_email", unique: true 62 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 63 end 64 65 add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" 66 add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" 67end 68

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/07/29 06:41
2022/07/29 07:43 編集
2022/07/29 06:55
2022/07/29 07:23
2022/07/29 07:45
2022/07/29 08:29
2022/07/30 12:30
2022/07/30 12:43