投稿機能実装中に発生したエラーが解決出来ないのでご教授お願い致します。
def show
@toppage = TopPage.find(params[:id])
@comment = Response.new
@comments = @toppage.responses.includes(:user)
end
個人的にこの部分が間違ってると思うのですがよくわからず。。。。
top_page>show.html.haml
ruby:qiita.rb
1.main 2 = form_for @comment, url:{controller: 'responses', action: 'create'}do |f| 3 .main__consultation 4 .main__consultation__title 5 お悩み一覧 6 .main__consultation__text 7 = @toppage.contents 8 .main__comment 9 = f.text_area :comment, class: "main__comment__form" 10 .main__comment__sent 11 = f.submit "投稿する" 12 .main__comment__text 13 <コメント一覧> 14 -if @comments 15 -@comments.each do |response| 16 .main__comment__main 17 .main__comment__main__name 18 = response.user.name 19 .main__comment__main__form 20 = response.comment
top_page.contoroller.rb
ruby:qiita.rb
1class TopPageController < ApplicationController 2 def index 3 @toppages = TopPage.all 4 end 5 6 def new 7 @toppage = TopPage.new 8 end 9 10 def create 11 TopPage.create(top_page_params) 12 redirect_to top_page_index_path 13 end 14 15 def show 16 @toppage = TopPage.find(params[:id]) 17 @comment = Response.new 18 @comments = @toppage.responses.includes(:user) 19 end 20 21 private 22 def top_page_params 23 params.require(:top_page).permit(:contents,:name).merge(user_id: current_user.id) 24 end 25end
responses_controller.rb
ruby:qiita.rb
1class ResponsesController < ApplicationController 2 def create 3 Response.create(response_params) 4 redirect_to root_path 5 end 6 7 private 8 def response_params 9 params.require(:response).permit(:comment).merge(user_id: current_user.id, top_page_id: params[:top_page_id]) 10 end 11end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/05 00:25
2020/08/05 02:17
2020/08/05 08:19
2020/08/05 10:45