Grantismo
Grantismo

Reputation: 3819

Problems with rails and saving to the database

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.

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

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.

Sentence.show

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

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.

edit, the view:

<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 %>

Upvotes: 1

Views: 107

Answers (1)

Patrick Klingemann
Patrick Klingemann

Reputation: 9014

ruby-debug will be very helpful:

gem install ruby-debug
script/server --debugger

Add the following after setting the @story in your show method:

def show
 @story = Story.find(params[:id])

 debugger
 @sentence_block =  @story.sentence_blocks.build

Then go to /stories/1 or whatever id you're using to test, the page will not finish loading, go to the terminal you're using for your server and you will see an irb prompt. You can run arbitrary code from that prompt and you can step through your code. Very useful.

Upvotes: 1

Related Questions