Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with rails and saving to the database
    primarykey
    data
    text
    <p>I've been having some difficulty in understanding the source of a problem. Below is a listing of the model classes. Essentially the goal is to have the ability to add sentences to the end of the story, or to add stories to an existing sentence_block. Right now, I'm only attempting to allow users to add sentences, and automatically create a new sentence_block for the new sentence. </p> <pre><code>class Story &lt; ActiveRecord::Base has_many :sentence_blocks, :dependent =&gt; :destroy has_many :sentences, :through =&gt; :sentence_blocks accepts_nested_attributes_for :sentence_blocks end class SentenceBlock &lt; ActiveRecord::Base belongs_to :story has_many :sentences, :dependent =&gt; :destroy end class Sentence &lt; ActiveRecord::Base belongs_to :sentence_block def story @sentence_block = SentenceBlock.find(self.sentence_block_id) Story.find(@sentence_block.story_id) end end </code></pre> <p>The problem is occurring when using the show method of the Story. The Story method is as follows, and the associated show method for a sentence is also included.</p> <p><strong>Sentence.show</strong></p> <pre><code>def show @sentence = Sentence.find(params[:id]) respond_to do |format| format.html {redirect_to(@sentence.story)} format.xml { render :xml =&gt; @sentence } end end </code></pre> <p><strong>Story.show</strong></p> <pre><code>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 =&gt; @story } else @sentence_block.destroy format.html format.xml { render :xml =&gt; @story } end end end </code></pre> <p>I'm getting a "couldn't find Sentence_block without and id" error. So I'm assuming that for some reason the sentence_block isn't getting saved to the database. Can anyone help me with my understanding of the behavior and why I'm getting the error? I'm trying to ensure that every time the view depicts show for a story, an unnecessary sentence_block and sentence isn't created, unless someone submits the form, I'm not really sure how to accomplish this. Any help would be appreciated.</p> <p>edit, the view:</p> <pre><code>&lt;p&gt; &lt;b&gt;Title:&lt;/b&gt; &lt;%=h @story.title %&gt; &lt;% @story.sentence_blocks.each do |b| %&gt; &lt;% b.sentences.each do |s| %&gt; &lt;br /&gt; &lt;%=h s.content %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;/p&gt; &lt;% form_for @new_sentence do |s| %&gt; &lt;p&gt; &lt;%= s.label :sentence %&gt;&lt;br /&gt; &lt;%= s.text_field :content %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= s.submit "Create" %&gt; &lt;/p&gt; &lt;% end %&gt; &lt;%= link_to 'Edit', edit_story_path(@story) %&gt; &lt;%= link_to 'Back', stories_path %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload