Note that there are some explanatory texts on larger screens.

plurals
  1. POCommenting model not saving and displaying :body section of comments
    primarykey
    data
    text
    <p>I'm new to rails and am having challenges adding a commenting system to my listings model. Effectively I have listings that are created by users, and I want the ability to allow other users to make comments on these listings. </p> <p>What I have so far:</p> <p>A listing model that includes:</p> <pre><code>has_many :comments </code></pre> <p>a comment model that includes:</p> <pre><code>belongs_to :listing </code></pre> <p>a comments controller:</p> <pre><code>class CommentsController &lt; 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 </code></pre> <p>and finally a listing view that includes the following code to collect and display comments:</p> <pre><code> &lt;div class="form-group"&gt; &lt;%= form_for [@listing, Comment.new] do |f| %&gt; &lt;%= f.label :comments %&gt; &lt;%= f.text_area :body, :placeholder =&gt; "Tell us what you think", class: "form-control", :rows =&gt; "3" %&gt; &lt;p&gt;&lt;%= f.submit "Add comment", class: "btn btn-primary" %&gt;&lt;/p&gt; &lt;% end %&gt; &lt;/div&gt; &lt;%= simple_form_for [@listing, Comment.new] do |f| %&gt; &lt;p&gt; &lt;%= f.input :body, :label =&gt; "New comment", as: :text, input_html: { rows: "3" } %&gt; &lt;/p&gt; &lt;p&gt;&lt;%= f.submit "Add comment", class: "btn btn-primary" %&gt;&lt;/p&gt; &lt;% end %&gt; </code></pre> <p>The comment box is displaying correctly in the view, and I'm able to submit comments, however it appears that the :body isn't being saved, and therefore the "submitted x minutes ago" is the only thing that's showing up in my comments section.</p> <p>Any ideas on what I could be doing incorrectly? I suspect it's a params issue, but haven't been able to work it out.</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload