我有一些控制器 - 用戶,類別,故事和註釋。一切都很好,直到我做了評論。在我的數據庫中,我想保存內容,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