문제의 원인을 이해하는 데 어려움을 겪고 있습니다. 다음은 모델 클래스 목록입니다. 근본적으로 목표는 문장의 끝에 문장을 추가하거나 기존 sentence_block에 기사를 추가하는 것입니다. 지금은 사용자가 문장을 추가 할 수 있도록 허용하려고 시도하고 새 문장의 새 문장 블록을 자동으로 작성합니다.레일 문제 및 데이터베이스에 저장
class Story < ActiveRecord::Base
has_many :sentence_blocks, :dependent => :destroy
has_many :sentences, :through => :sentence_blocks
accepts_nested_attributes_for :sentence_blocks
end
class SentenceBlock < ActiveRecord::Base
belongs_to :story
has_many :sentences, :dependent => :destroy
end
class Sentence < ActiveRecord::Base
belongs_to :sentence_block
def story
@sentence_block = SentenceBlock.find(self.sentence_block_id)
Story.find(@sentence_block.story_id)
end
end
이야기의 show 메서드를 사용할 때 문제가 발생합니다. Story 메서드는 다음과 같으며 문장과 관련된 show 메서드도 포함되어 있습니다.
Sentence.show 나는 오류 "및 ID없이 Sentence_block를 찾을 수 없습니다"얻고
def show
@sentence = Sentence.find(params[:id])
respond_to do |format|
format.html {redirect_to(@sentence.story)}
format.xml { render :xml => @sentence }
end
end
Story.show
def show
@story = Story.find(params[:id])
@sentence_block = @story.sentence_blocks.build
@new_sentence = @sentence_block.sentences.build(params[:sentence])
respond_to do |format|
if @new_sentence.content != nil and @new_sentence.sentence_block_id != nil and @sentence_block.save and @new_sentence.save
flash[:notice] = 'Sentence was successfully added.'
format.html # new.html.erb
format.xml { render :xml => @story }
else
@sentence_block.destroy
format.html
format.xml { render :xml => @story }
end
end
end
. 그래서 나는 어떤 이유로 sentence_block이 데이터베이스에 저장되지 않는다고 가정하고 있습니다. 아무도 내 행동의 이해와 왜 내가 오류가 점점 도와 줄래? 보기가 이야기를 보여줄 때마다 누군가가 양식을 제출하지 않으면 불필요한 sentence_block 및 문장이 작성되지 않도록 확실히하기 위해 노력하고 있습니다. 실제로이 작업을 수행하는 방법을 잘 모르겠습니다. 어떤 도움을 주시면 감사하겠습니다.
편집,보기 :
<p>
<b>Title:</b>
<%=h @story.title %>
<% @story.sentence_blocks.each do |b| %>
<% b.sentences.each do |s| %>
<br />
<%=h s.content %>
<% end %>
<% end %>
</p>
<% form_for @new_sentence do |s| %>
<p>
<%= s.label :sentence %><br />
<%= s.text_field :content %>
</p>
<p>
<%= s.submit "Create" %>
</p>
<% end %>
<%= link_to 'Edit', edit_story_path(@story) %>
<%= link_to 'Back', stories_path %>
Story.show는 실제로 '보이지 않습니다'... 어쩌면 그 메소드를 create (새로운 문장을 작성한다는 의미)라고해야합니다. – pgmura