简体   繁体   中英

What would cause an action in my Ruby on Rails controller to be called repeatedly?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM