控制器中的堆栈级别太深

时间:2015-05-05 21:01:03

标签: ruby-on-rails-4

我已经阅读了很多有关此错误的问题,但没有一个解决方案相关或有帮助。

我理解这个错误意味着我在某个地方有一个无限循环,但在哪里?

模型:

class Book < ActiveRecord::Base

    belongs_to :writer, class_name: 'User', foreign_key: 'writer_id'
    belongs_to :address, class_name: 'UserAddress', foreign_key: 'user_address_id'
    has_many :stories

    after_validation :geocode
    geocoded_by :address_str


    def address_str
        address.address
    end
end


class Story < ActiveRecord::Base

    belongs_to :story_book
    belongs_to :address, class_name: 'UserAddress', foreign_key: 'user_address_id'

    after_validation :geocode
    geocoded_by :address_str

    include Workflow

    workflow do
        state :pending do
            event :approve, :transition_to => :approved
            event :cancel, :transition_to => :cancelled
        end
        state :approved do
            event :close, :transition_to => :closed
            event :cancel, :transition_to => :cancelled
        end
    end

    def address_str
        address.address
    end
end

class UserAddress < ActiveRecord::Base

    belongs_to :user
    has_many :books
    has_many :stories

end

控制器:

class StoriesController < ApplicationController
    def create
        if params['book_id'].present?
            @book = Book.find params['book_id'].to_i
            if params['book'].present?
                @book.update(update_book_params)
            end
        else
            @book = current_user.books.create(create_book_params)
        end

        if @book.persisted?
            @story = @book.stories.new(story_params)
        else
            @story = Story.new(story_params)
        end

        respond_to do |format|
            if @story.save
              format.js   { render json: @story}
            else
              format.js { render json: @story.errors, status: :unprocessable_entity }
            end
        end
    end

    private
    def create_book_params
      params.require(:book).permit(:writer_id,:pages, :rate, :title, :approved, :user_address_id)
    end

    def update_book_params
      params.require(:book).permit( :rate, :title, :approved, :user_address_id)
    end

    def story_params
        params.require(:story).permit(:rate, :title, :text, :from_page, :to_page, :user_address_id)
    end
end

似乎它正在访问@ story.save,创建记录然后回滚事务

服务器日志:

[2015-05-05T23:00:24.327187 #13269] DEBUG -- :    (0.8ms)  ROLLBACK
I, [2015-05-05T23:00:24.327597 #13269]  INFO -- : Completed 500  Internal Server Error in 1138ms
F, [2015-05-05T23:00:24.328006 #13269] FATAL -- : 
SystemStackError (stack level too deep):
  activerecord (4.1.6)     lib/active_record/connection_adapters/abstract/connection_pool.rb:629
I, [2015-05-05T23:00:24.502318 #13269]  INFO -- :   Rendered   /home/avital/.rvm/gems/ruby-1.9.3-p547/gems/actionpack- 4.1.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
I, [2015-05-05T23:00:24.506788 #13269]  INFO -- :   Rendered /home/avital/.rvm/gems/ruby-1.9.3-p547/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (0.8ms)
I, [2015-05-05T23:00:24.511707 #13269]  INFO -- :   Rendered /home/avital/.rvm/gems/ruby-1.9.3-p547/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.8ms)
I, [2015-05-05T23:00:24.511825 #13269]  INFO -- :   Rendered /home/avital/.rvm/gems/ruby-1.9.3-p547/gems/actionpack-4.1.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (54.3ms)
127.0.0.1 - - [05/May/2015 23:00:24] "POST /new_story HTTP/1.1" 500 5466 1.7481

堆栈跟踪为空。

我错过了什么?

谢谢!

修改

出于某种原因,我从上面的代码中删除了这个Story方法:

def comment
  (book.comment.present? ? "#{book.comment}\n#{comment}" : comment)
end

1 个答案:

答案 0 :(得分:0)

无限循环位于Story#comment方法中,旨在覆盖Story的注释属性(请参阅问题中的错误实现)。

正确的方式:

def comment
  (book.comment.present? ? "#{book.comment}\n#{read_attribute(:comment)}" : read_attribute(:comment))
end