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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

1306閲覧

コメントを投稿できるようにしたいが、form_withの部分でエラーが出ます。

sewwolf

総合スコア4

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2021/11/29 17:07

前提・実現したいこと

チャットをフォームから入力して投稿させるのが目的です。

発生している問題・エラーメッセージ

投稿ボタンを押すと下のエラーが出ます。#show.html.erbの<%= form_with (model: [@prototype, @comment],local: true) do |f|%>の部分でよくエラーが出ます。

ActionView::SyntaxErrorInTemplate in PrototypesController#show Encountered a syntax error while rendering template: check <main class="main"> <div class="inner"> <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= @prototype.title %> </p> <%= link_to "by #{@prototype.user.name}", root_path, class: :prototype__user %> <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> <% if user_signed_in? && current_user.id == @prototype.user_id %> <div class="prototype__manage"> <%= link_to "編集する", edit_prototype_path, class: :prototype__btn %> <%= link_to "削除する", prototype_path(@prototype.id), method: :delete, class: :prototype__btn %> </div> <% end %> <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> <div class="prototype__image"> <%= image_tag @prototype.image %> </div> <div class="prototype__body"> <div class="prototype__detail"> <p class="detail__title">キャッチコピー</p> <p class="detail__message"> <%= @prototype.catch_copy %> </p> </div> <div class="prototype__detail"> <p class="detail__title">コンセプト</p> <p class="detail__message"> <%= @prototype.concept %> </p> </div> </div> <div class="prototype__comments"> <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> <% if user_signed_in? %> <%= form_with (model: [@prototype, @comment],local: true) do |f|%> <div class="field"> <%= f.label :content, "コメント" %><br /> <%= f.text_field :content, id:"comment_content" %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <% end %> <%# // ログインしているユーザーには上記を表示する %> <ul class="comments_lists"> <%# 投稿に紐づくコメントを一覧する処理を記述する %> <li class="comments_list"> <%# <%= " コメントのテキスト "%> <%# <%= link_to "( ユーザー名 )", root_path, class: :comment_user %> </li> <%# // 投稿に紐づくコメントを一覧する処理を記述する %> </ul> </div> </div> </div> </main>

該当のソースコード

ruby

1#routes.rb 2devise_for :users 3 root to: "prototypes#index" 4 resources :prototypes do 5 resources :comments, only: [:create] 6 end 7

ruby

1#comments_controller.rb 2def create 3 4 @comment = Comment.new(comment_params) 5 if @comment.save 6 redirect_to prototype_path(@comment.tweet) 7 else 8 @prototype = @comment.prototype 9 @comments = @prototype.comments 10 render "prototypes/show" 11 end 12 end 13 14 private 15 16 def comment_params 17 params.require(:comment).permit(:content).merge(user_id: current_user.id, prototype_id: params[:prototypes_id]) 18 end

ruby

1#prototypes_controller.rb 2 def index 3 @prototypes = Prototype.all 4 end 5 6 def new 7 @prototype = Prototype.new 8 end 9 10 def create 11 @prototype = Prototype.new(prototype_params) 12 if @prototype.save 13 redirect_to root_path(@prototype) 14 else 15 render :new 16 end 17 end 18 19 def show 20 @prototype = Prototype.find(params[:id]) 21 @comment = Comment.new 22 @comments = @prototype.comments.includes(:user) 23 end 24 25 def edit 26 @prototype = Prototype.find(params[:id]) 27 end 28 29 def update 30 @prototype = Prototype.find(params[:id]) 31 if @prototype.update(prototype_params) 32 redirect_to root_path 33 else 34 render :edit 35 end 36 end 37 38 def destroy 39 prototype = Prototype.find(params[:id]) 40 prototype.destroy 41 redirect_to root_path 42 43 end 44 private 45 46 def prototype_params 47 params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 48 end 49

ruby

1#show.html.erb 2<% if user_signed_in? %> 3 <%= form_with (model: [@prototype, @comment],local: true) do |f|%> 4 <div class="field"> 5 <%= f.label :content, "コメント" %><br /> 6 <%= f.text_field :content, id:"comment_content" %> 7 </div> 8 <div class="actions"> 9 <%= f.submit "送信する", class: :form__btn %> 10 </div> 11 <% end %>

試したこと

同じような質問の確認。コントローラーとルーティング等の見直し。
何回も調べたり、確認をしたりしましたが、わかりませんでした。

補足情報(FW/ツールのバージョンなど)

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

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

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

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

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

guest

回答1

0

ベストアンサー

ActionView::SyntaxErrorInTemplate だから、文字通り、文法エラーですね。

おそらく、入ってはいけない空白が入っているのが問題だと推測します。以下でどうでしょうか。

erb

1<%= form_with(model: [@prototype, @comment],local: true) do |f| %> 2 <%# ↑ 原文はここに余計な空白がある %>

投稿2021/11/29 21:12

MasaSakano

総合スコア188

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

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

sewwolf

2021/11/30 11:07

ありがとうございます。解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問