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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

823閲覧

SintaxErrorが解決できません

pi-nattu

総合スコア61

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2022/07/29 05:15

編集2022/07/30 21:43

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

views/users/show.html.erb の、以下の①の<% end %>は不要では無いでしょうか?

ruby

1 <%= image_tag @user.get_profile_image(100,100) %> 2 <% if @user.id == current_user.id %> 3 <p><%= link_to "プロフィール編集", edit_user_path(@user) %></p> 4 <% end %> 5 <% end %> ・・・①これが不要?

app/views/_books も以下の②〜⑤を見直したほうがいいかもです

ruby

1<% books.each do |book| %>  ・・・②対応する<% end %>が存在しない? 2<div> 3 <%= link_to user_path(book.id) do %> 4 <P><%= image_tag book.user.get_profile_image(100,100) %></P> 5 <% end %> 6 <h3>Title</h><br> ・・・ ③</h>ではなく</h3> 7 <%= link_to book.title, book_path(book.id) %></p> ・・・④この</p>はどの<p>の閉じタグ? 8 <h3>Opinion</h><br> ・・・ ⑤</h>ではなく</h3> 9 <%= book.body %> 10</div>

もう少しインデントを揃えれば、この辺りの間違いに気づきやすくなりますよ!

あと今回の質問とは関係ないのですが、気になったのでついでに・・・
views/books/index.html.erbは、
<thead>の中に<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/・・・>がありますが、
<thead>の中に<link>を入れることはできません。
通常、<link><head>の中に記述する、で問題ないかと思います。

投稿2022/07/29 06:32

nae_stage

総合スコア274

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

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

pi-nattu

2022/07/29 06:41

+αの回答までして頂きありがとうございます! まだプログラミングの勉強始めて2ヶ月経たないくらいなので、色々試しているうちに分からなくなってしまっていました... 試してみます! theadはtextのhead(という認識)で同じように出来ると思っていたのですがまた違うんですね。
nae_stage

2022/07/29 07:43 編集

> theadはtextのhead(という認識)で同じように出来ると思っていたのですがまた違うんですね。 答えになってるかわかりませんが、 headはHTML文書のヘッダー要素(ブラウザ上には表示されない様々な設定などを記述する場所)で、 (参考:https://developer.mozilla.org/ja/docs/Web/HTML/Element/head ) theadはただの表のヘッダー要素(ブラウザ上に表示する表のタイトルなどを記述する場所)ですので、 (参考:https://developer.mozilla.org/ja/docs/Web/HTML/Element/thead ) 全く別物ですね。
nae_stage

2022/07/29 06:55

どの要素の中にどの要素を入れるのがOKでどの要素がNGなのか忘れた時、 私は以下のツールを使って確認してます!参考まで・・・ https://yoshikawaweb.com/element/
pi-nattu

2022/07/29 07:23

上2つのものはリンクが切れていましたが、なるほど、そういえばカリキュラムでも同じことが書いてあった気がします。勉強不足でした。 そしてツールの紹介までありがとうございます!凄く見やすいですね! 出先でまだプログラミング作業出来ないのですがベストアンサーにさせていただきます! 本当に丁寧に教えてくださりありがとうございます!
nae_stage

2022/07/29 07:45

> 上2つのものはリンクが切れていましたが すみません、直しました!🙇‍♀️
pi-nattu

2022/07/29 08:29

リンクの修正ありがとうございます! こちらもわかりやすいのと色々廃止されているものもあるんですね。勉強になります!
pi-nattu

2022/07/30 12:30

指摘されたとおりに修正してみたところエラー解決いたしました! 本当にありがとうございました!
nae_stage

2022/07/30 12:43

良かったです!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問