しばらくの間、ブログのコメント システムに行き詰まっていました。問題を解決し、私を正しい方向に導くために、あなたの助けが必要です。Ruby on rails と twitter オープン ソース フレームワーク ブートストラップを使用しています。
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>
コメントコントローラー:
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
コメント.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
show.html.erb で、2 つの (:body) シンボルと form_for 行でエラーが発生します。
これは私の最初のブログであり、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
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 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 %>
現在のエラー:
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'