我的博客上的评论系统已经卡了一段时间了。我现在需要你的帮助来解决问题并将我推向正确的方向。我正在使用 Ruby on rails 和 twitter 开源框架引导程序。
显示.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>
评论控制器:
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create!(params[:comment])
redirect_to @post
end
end
路由.rb
Blog2::Application.routes.draw do
resources :posts do
resources :comments, :only => [:create]
end
评论.html.erb
<%= div_for comment do %>
<p>
<strong>
Posted <%= time_ago_in_words(comment.created_at) %> ago
</strong>
<br/>
<%= comment.body %>
</p>
<% end %>
模型/评论.rb
class Comment < ActiveRecord::Base
belongs_to :post
attr_accessible :body
end
我的两个 (:body) 符号和 form_for 行在 show.html.erb 中出现错误。
好吧,这是我的第一篇博客,我对 RoR 还很陌生,感谢一些帮助 :)
第一个错误:
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
第二个错误
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"}
在遵循 zeantsoi 的建议后,我更改了 show.html.erb 和 _comment.html.erb。这就是我现在剩下的地方 15/6 22.03 更新!
更新了 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 %>
当前错误:
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'