Stack Level Too Deep错误 - 我认为使用强参数生成

时间:2014-08-28 11:21:00

标签: ruby-on-rails strong-parameters

我认为问题可能是Rails 4中的强参数,但我不确定。

这就是我所拥有的。

我要做的就是创建一个post,但是当它被提交时我会收到此错误:

Started POST "/posts" for 127.0.0.1 at 2014-08-28 06:06:57 -0500
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"daURJjzq2EMfiQZ/uiD/0ADg=", "post"=>{"status"=>"confirmed", "title"=>"Ashlee lost 10 pounds in 5 weeks", "photo"=>#<ActionDispatch::Http::UploadedFile:0x0000010652d3c8 @tempfile=#<Tempfile:/var/folders/0f/hgplttnd7dg6q9m62qtbnpn00000gn/T/RackMultipart20140828-87750-gyz4np>, @original_filename="Ashlee-Testimonial.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"post[photo]\"; filename=\"Ashlee-Testimonial.png\"\r\nContent-Type: image/png\r\n">, "body"=>"Ashlee lost 10 pounds in 5 weeks."}, "commit"=>"Submit"}
  User Load (0.5ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
   (0.4ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1]]
Completed 500 Internal Server Error in 77ms

SystemStackError - stack level too deep:
  actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:79:in `'

Post模型如下所示:

# == Schema Information
#
# Table name: posts
#
#  id         :integer          not null, primary key
#  status     :string(255)
#  title      :string(255)
#  date       :datetime
#  image      :string(255)
#  body       :text
#  created_at :datetime
#  updated_at :datetime
#  user_id    :integer
#  ancestry   :string(255)
#  file       :string(255)
#

class Post < ActiveRecord::Base
  has_ancestry
  belongs_to :user
  resourcify

  mount_uploader :photo, ImageUploader
  mount_uploader :file, FileUploader
end

我的PostsController看起来像这样:

  def create
    @post = current_user.posts.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:status, :title, :date, :photo, :body, :parent_id)
    end

这是我的表格部分看起来的样子:

<%= simple_form_for @post do |f| %> 
    <%= f.error_notification %>

    <%= f.input :parent_id, as: :hidden %>

    <% if can? :manage, @post %>
      <%= f.input :status, collection: Status.all %>
    <% end %>       

    <%= f.input :title %><br />
    <%= f.input :date %><br />      
    <%= f.input :photo %><br />
    <%= f.input :body %><br />      

  <%= f.button :submit %>

<% end %>

关于可能导致此错误的任何想法?

更新1:

当我创建一个没有上传文件的帖子时,它创建正常,如下面的日志所示:

Started POST "/posts" for 127.0.0.1 at 2014-08-28 06:34:50 -0500
Processing by PostsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"daUAvh6w4dCH3sswJ6dckYRJjzq2EMfiQZ/uiD/0ADg=", "post"=>{"status"=>"confirmed", "title"=>"Test Post", "body"=>"Does this work at all."}, "commit"=>"Submit"}
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = 1  ORDER BY "users"."id" ASC LIMIT 1
   (0.4ms)  SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))  [["user_id", 1]]
   (0.4ms)  BEGIN
  SQL (2.2ms)  INSERT INTO "posts" ("body", "created_at", "status", "title", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["body", "Does this work at all."], ["created_at", "2014-08-28 11:34:50.886549"], ["status", "confirmed"], ["title", "Test Post"], ["updated_at", "2014-08-28 11:34:50.886549"], ["user_id", 1]]
   (0.8ms)  COMMIT
Redirected to http://localhost:3000/posts/2
Completed 302 Found in 83ms (ActiveRecord: 4.1ms)

所以看起来罪魁祸首实际上是在那里的Carrierwave / Fog / Strong Parameters中。

更新2:

的Gemfile:

source 'https://rubygems.org'

gem 'rails', '4.1.1'

group :assets do
  gem 'sass-rails', '~> 4.0.3'
  gem 'uglifier', '>= 1.3.0'
  gem 'coffee-rails', '~> 4.0.0'
  gem "font-awesome-rails"
  gem 'bootstrap-sass', '~> 3.2.0'
  gem 'autoprefixer-rails'
end

group :development do
    gem 'annotate', github: 'ctran/annotate_models'
    gem 'sextant'
  gem "quiet_assets", ">= 1.0.2"
  gem 'better_errors', '~> 1.1.0'
  gem 'binding_of_caller', '~> 0.7.2'
    gem 'meta_request'
    gem 'execjs'
    gem 'therubyracer'  
  gem "letter_opener"
  gem 'bullet'   
  gem 'rack-mini-profiler'     
  gem 'guard-rails'
  gem 'rb-fchange', :require=>false
  gem 'rb-fsevent', :require=>false
  gem 'rb-inotify', :require=>false
  gem 'guard-livereload', '~> 2.3.0', :require=>false
  gem 'rack-livereload', '~> 0.3.15'
end

group :production do
  gem 'rails_12factor'
end

gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0',          group: :doc
gem 'spring',        group: :development
gem 'devise', '~> 3.2.4'
gem 'thin'
gem 'pg'
gem 'cancancan', '~> 1.8.2'
gem 'rolify'
gem "rmagick"
gem "mini_magick"
gem 'carrierwave', '~> 0.10.0'
gem "fog", "~> 1.3.1"
gem 'figaro', '~> 0.7.0'
gem 'geocoder', '~> 1.2.2'
gem 'social-share-button', '~> 0.1.6'
gem 'ancestry', '~> 2.1.0'
gem "simple_form"

更新3:

所以似乎已经修复了这个特定的Stack Level Too Deep Error,但问题刚刚发生了变化。我在这里创建了一个新的SO问题 - Exconn::Errors::SocketError in file upload via Carrierwave and Fog

2 个答案:

答案 0 :(得分:2)

尝试this

在您的gemfile中

,将gem "rmagick"更改为

gem 'rmagick', :require => 'RMagick'

来自https://github.com/carrierwaveuploader/carrierwave/issues/1330

答案 1 :(得分:1)

问题在于您的数据库。您正在为photo设置强参数和装载上传器,但您的数据库包含名为image的列。尝试将列名更改为photo可以解决您的问题。如果你正在做任何调整大小的东西,rmagick也需要。