我有一些控制器 - 用户,类别,故事和注释。一切都很好,直到我做了评论。在我的数据库中,我想保存内容,user_id,story_id,但表格是空的。 @ comment.save是错误的。这里是我的代码部分:rails无法将结果保存在数据库中
CommentsController:
def create
@story = Story.find(params[:story_id])
@comment = @story.comments.create(params[:comment])
if @comment.save
flash[:success] = "Successfull added comment"
redirect_to stories_path
else
render 'new'
end
end
show.html.erb为StoriesController:
<b><%= @story.title %></b> <br/><br/>
<%= @story.content %> <br/><br/>
<% @story.comments.each do |comment| %>
<b>Comment:</b>
<%= comment.content %>
<% end %>
<%= form_for([@story, @story.comments.build]) do |f| %>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit "Add" %>
</div>
<% end %>
在StoriesController我做同样的事情,但我现在不知道如何做到这一点。
def create
@categories = Category.all
@story = current_user.stories.build(params[:story])
end
哪一行明确导致错误假的?你是否在故事和评论之间建立了联系? – ellawren 2012-03-02 23:33:14
我不知道如何,但当我重新启动服务器的错误消息的问题已修复,但评论表我DB是空的。我在story.rb has_many:评论和评论.rb - belongs_to:故事。在route.rb我: 资源:故事做 资源:评论 结束 – user1107922 2012-03-03 00:11:06
现在我再次有错误消息..这行:@comment = current_user.comments.create(PARAMS [:评论]) – user1107922 2012-03-03 00:21:03