views:

22

answers:

1

Helllo. I'm diving into RoR and, after installing Paperclip and setting up my code to adapt to it, the new method in one of my controllers is causing a StackOverflow ;) . I'm running Rails 3.0 and Ruby 1.8.7. Can anyone guess or tell me what could be causing my new action to be called repeatedly?

Here's the server output...

Started GET "/projects/new" for 127.0.0.1 at Mon Oct 18 19:25:42 -0500 2010
  Processing by ProjectsController#new as HTML
Completed   in 1192ms

SystemStackError (stack level too deep):
  app/controllers/projects_controller.rb:27:in `new'
  app/controllers/projects_controller.rb:27:in `new'
  app/controllers/projects_controller.rb:30:in `new'
  app/controllers/projects_controller.rb:29:in `new'
  .
  . 
  .
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (3.6ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (4.3ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (63.9ms)

Here's my controller code...

def new
    @project = Project.new

    respond_to do |format|
      format.html new.html.erb
      format.xml  { render :xml => @project }
    end
  end

Here's my view code for the new template...

<%= form_for(:project, @project, :html => {:id => "project_form", :multipart => true}) do |f| %>
  <% if @project.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>

      <ul>
      <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :tech_used %><br />
    <%= f.text_field :tech_used %>
  </div>
  <div class="field">
    <%= f.label :owner %><br />
    <%= f.text_field :owner %>
  </div>
  <div class="field">
    <%= f.label :created %><br />
    <%= f.date_select :created %>
  </div>
 <div class="field">
  <%= f.file_field :thumbnail %>
 </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Thanks for your help!

+1  A: 

Use the following:

def new
  @project = Project.new

  respond_to do |format|
    format.html
    format.xml  { render :xml => @project }
  end
end

With format.html new.html.erb, you're invoking the new method again; if it ever returned you'd be invoking the .html method, which would cause a method-not-found error. Instead it recursively calls new until you run out of memory.

meagar
thanks so much, that fixed it. i don't understand. without specifying a template, i assume it just calls the template with same name as the action? if so, i still don't understand why specifying the template invokes the new method again.
BeachRunnerJoe
@Beach Because you're not specifying the template, you're calling `new`. If you want to specify the template, use a string, with quotes: `"new.html.erb"`.
meagar
i see, thank you!
BeachRunnerJoe