Rails は初めてで、リスティング モデルにコメント システムを追加するのに苦労しています。事実上、ユーザーによって作成されたリストがあり、他のユーザーがこれらのリストにコメントできるようにしたいと考えています。
私がこれまでに持っているもの:
以下を含むリスティング モデル:
has_many :comments
以下を含むコメントモデル:
belongs_to :listing
コメント コントローラ:
class CommentsController < ApplicationController
def create
@listing = Listing.find(params[:listing_id])
@comment = @listing.comments.build(params[:body]) # ***I suspected that I needed to pass :comment as the params, but this throws an error. I can only get it to pass with :body ***
respond_to do |format|
if @comment.save
format.html { redirect_to @listing, notice: "Comment was successfully created" }
format.json { render json: @listing, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
end
def comment_params
params.require(:comment).permit(:body, :listing_id)
end
最後に、コメントを収集して表示する次のコードを含むリスト ビューです。
<div class="form-group">
<%= form_for [@listing, Comment.new] do |f| %>
<%= f.label :comments %>
<%= f.text_area :body, :placeholder => "Tell us what you think", class: "form-control", :rows => "3" %>
<p><%= f.submit "Add comment", class: "btn btn-primary" %></p>
<% end %>
</div>
<%= simple_form_for [@listing, Comment.new] do |f| %>
<p>
<%= f.input :body, :label => "New comment", as: :text, input_html: { rows: "3" } %>
</p>
<p><%= f.submit "Add comment", class: "btn btn-primary" %></p>
<% end %>
コメント ボックスはビューに正しく表示され、コメントを送信できますが、:body が保存されていないように見えるため、「x 分前に送信されました」だけが表示されます。コメント欄。
私が間違っている可能性があることについてのアイデアはありますか? パラメータの問題だと思いますが、解決できませんでした。
ありがとう!