简体   繁体   中英

A blog using Bootstrap and Ruby on rails

I've been stuck for a time now with my commenting system on my blog. I now need you help to solv the problem and push me in the right direction. I'm using Ruby on rails and the twitter open source framework bootstrap.

show.html.erb:

    <h2>Comments</h2>
 <div id="comments">
         <%= render :partial => @post.comments %>
 </div>

 <%= form_for {@post, Comment.new} do |f| %>
        <p>
                <%= f.label :body, "New comment" %><br/>
                <%= f.text_area :body %>
        </p>
        <p><%= f.submit "Add comment" %></p>
<% end %>

</div>

CommentsController:

class CommentsController < ApplicationController
        def create
                @post = Post.find(params[:post_id])
                @comment = @post.comments.create!(params[:comment])
                redirect_to @post
        end
end

Routes.rb

Blog2::Application.routes.draw do
  resources :posts do
    resources :comments, :only => [:create]
  end

comment.html.erb

<%= div_for comment do %>
        <p>
                <strong>
                        Posted <%= time_ago_in_words(comment.created_at) %> ago
                </strong>
                <br/>
                <%= comment.body %>
        </p>
<% end %>

model/ comment.rb

class Comment < ActiveRecord::Base
  belongs_to :post
  attr_accessible :body
end

I'm getting errors in show.html.erb for my two (:body) symbols and the form_for line.

Well this is my first blog and I'm pretty new to RoR, appreciate some help :)

1st Error:

 SyntaxError in Posts#show

Showing /Users/Jdartland/blog_2/app/views/posts/show.html.erb where line #23 raised:

/Users/Jdartland/blog_2/app/views/posts/show.html.erb:23: syntax error, unexpected '}', expecting tCOLON2 or '[' or '.'
...  form_for {@post, Comment.new} do |f| @output_buffer.safe_c...
...                               ^
/Users/Jdartland/blog_2/app/views/posts/show.html.erb:29: syntax error, unexpected keyword_end, expecting '}'
'); end 
       ^
/Users/Jdartland/blog_2/app/views/posts/show.html.erb:32: syntax error, unexpected keyword_ensure, expecting '}'
/Users/Jdartland/blog_2/app/views/posts/show.html.erb:34: syntax error, unexpected keyword_end, expecting '}'
Extracted source (around line #23):

20:          <%= render :partial => @post.comments %>
21:  </div>
22: 
23:  <%= form_for {@post, Comment.new} do |f| %>
24:         <p>
25:                 <%= f.label :body, "New comment" %><br/>
26:                 <%= f.text_area :body %>
Trace of template inclusion: app/views/posts/show.html.erb

Rails.root: /Users/Jdartland/blog_2

2nd Error

ArgumentError in Posts#show

Showing /Users/Jdartland/blog_2/app/views/posts/show.html.erb where line #23 raised:

wrong number of arguments (1 for 0)
Extracted source (around line #23):

20:          <%= render :partial => @post.comments %>
21:  </div>
22: 
23:  <%= form_for @post, Comment.new do |f| %>
24:         <p>
25:                 <%= f.label :body, "New comment" %><br/>
26:                 <%= f.text_area :body %>
Rails.root: /Users/Jdartland/blog_2

Application Trace | Framework Trace | Full Trace
app/views/posts/show.html.erb:23:in `_app_views_posts_show_html_erb___3910729666325401247_70293324213060'
app/controllers/posts_controller.rb:26:in `show'
Request

Parameters:

{"id"=>"2"}

After following zeantsoi's advice I changed the show.html.erb and _comment.html.erb. This is where I am left with right now 15/6 22.03 UPDATED!

Updated show.html.erb:

<h2>Comments</h2>
 <div id="comments">s
         <%= render :partial => 'posts/comment', :locals => {:comments => @post.comments} %>
         <p>
  </div>
</div>

<%= form_for [@post, Comment.new] do |f| %>
  <p>
                <%= f.label :body, "New comment" %><br/>
                <%= f.text_area :body %>

        <%= f.submit "Add comment" %>
  </p>
<% end %> 

The current Error:

howing /Users/Jdartland/blog_2/app/views/posts/show.html.erb where line #28 raised:

undefined method `body' for #<Comment:0x007f88611a1738>
Extracted source (around line #28):

25: <%= form_for [@post, Comment.new] do |f| %>
26:   <p>
27:                 <%= f.label :body, "New comment" %><br/>
28:                 <%= f.text_area :body %>
29:         
30:         <%= f.submit "Add comment" %>
31:   </p>
Rails.root: /Users/Jdartland/blog_2

Application Trace | Framework Trace | Full Trace
app/views/posts/show.html.erb:28:in `block in _app_views_posts_show_html_erb___3992012847724603598_70111860057680'
app/views/posts/show.html.erb:25:in `_app_views_posts_show_html_erb___3992012847724603598_70111860057680'
app/controllers/posts_controller.rb:26:in `show'

不知道这是否可以解决您所有的问题,但是在您的情况下,您需要传递这样的数组

 form_for [@post, Comment.new]

First, you need to pass a partial path to your render :partial , not an object(s):

# app/views/posts/show.html.erb (for a partial located at `app/views/posts/_comments.html.erb`
<%= render :partial => 'posts/comments', :locals => {:comments => @post.comments} %>

# app/views/posts/_comments.html.erb
<% comments.each do |comment| %>
    <%= comment.content %>
<% end %>

Second, in order to send your post to your nested route, you'll need to wrap your form_for arguments in brackets, not curly braces:

# app/views/posts/show.html.erb
<%= form_for [@post, Comment.new] do |f| %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM