사용자, 카테고리, 이야기 및 댓글과 같은 컨트롤러가 있습니다. 내가 의견을 말할 때까지는 모든 것이 괜찮았다. 내 DB에서 content, user_id, story_id를 저장하려고하지만 테이블이 비어 있습니다. @ comment.save는 false입니다. 여기에 내 코드의 일부는 다음과 같습니다레일이 DB에 결과를 저장할 수 없습니다.
CommentsController : StoriesController에 대한
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 %>
방법 해.
는
def create
@categories = Category.all
@story = current_user.stories.build(params[:story])
end
거짓이다? 이야기와 댓글 사이에 조인을 설정 했습니까? – ellawren
어떻게해야할지 모르겠지만 서버를 다시 시작하면 오류 메시지의 문제가 해결되었지만 Comments 테이블은 DB가 비어 있습니다. 그리고 나는 story.rb has_many : comment와 comment.rb - belongs_to : story에있다. route.rb에서 내가 가진 : 자원 : 이야기 자원을 수행 최종 – user1107922
가 지금은 다시 오류 메시지가 코멘트 ..를이 라인에서 : @comment = current_user.comments.create (PARAMS는 [: 코멘트]) – user1107922