2016-08-26 66 views
0
  • 的Ubuntu 16.04.1 LTS
  • 滑軌5.0.0
  • 紅寶石2.3.1p112(2016年4月26日)[x86_64的-Linux的GNU]
  • 寶石 'mysql2', '〜> 0.3.18',:平臺=>:紅寶石
  • 寶石思維 - 獅身人面像', '〜> 3.2.0'
  • 的PostgreSQL 9.5.3

我設立獅身人面像搜索引擎在我的Rails 5項目和它(可能)打破了Rails accepting_attributes_for方法。至少accepts_nested_attributes_for方法僅適用,如果我在的Gemfile設置這些行:accepts_nested_attributes_for取決於思考斯芬克斯(錯誤?)

gem 'mysql2',   '~> 0.3.18', :platform => :ruby 
gem 'thinking-sphinx', '~> 3.2.0' 

沒有這些線路accepts_nested_attributes_for不起作用! 「不工作」的意思是嵌套模型不是通過父創建表單創建(我得到的錯誤:

1 error prohibited this user from being saved: Articles user must exist

我上傳我的項目的Heroku和問題發生有太多 - 完全一樣的方式(儘管Heroku不支持免費的Sphinx)。

這是我在GitHub上的測試項目:https://github.com/dorelly2/test-project。這只是一個腳手架生成的測試項目。

我做了2個Heroku應用程序,它演示了不同行爲取決於2行(上面提到的)是否在Gemfile中被註釋掉了。你可以和他們一起玩,看看。嘗試創建一個用戶在這兩個應用NameArticle title,你會看到其中的差別:

https://test-project-ts.herokuapp.com/

https://test-project-ts-bad.herokuapp.com/

下面是代碼示例:

class User < ApplicationRecord 
    has_many :articles, dependent: :destroy 
    accepts_nested_attributes_for :articles 
end 

class Article < ApplicationRecord 
    belongs_to :user 
end 

觀點:

<%= form_for(user) do |f| %> 
... 

    <div class="field"> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 
    </div> 

    <%= f.fields_for :articles, Article.new do |ff| %> 
    <div class="field"> 
     <%= ff.label "Article title" %> 
     <%= ff.text_field :title %> 
    </div> 
    <% end %> 
... 

控制器是腳手架生成的 - 無法在這裏顯示它們。

發生了什麼事?有人可以清理嗎? accep_nested_attributes_for一般沒有思考斯芬克斯的工作?

回答

1

這實際上是Rails/ActiveRecord 5.0中的行爲變化,思維獅身人面像中的一個錯誤使它停止工作:belongs_to關聯默認爲進行驗證。

TS中的錯誤是固定的,儘管新版本尚未發佈該修補程序。詳細在這裏:Thinking-spinx breaks belongs_to built-in validation

+0

不,它沒有幫助。我安裝了您的固定TS和** accepts_nested_attributes_for **,甚至停止使用它。你能否下載我的源代碼(來自Git)並將它們放在你的電腦上? – prograils

+0

我在想這個bug實際上位於什麼位置 - 在receiving_nested_attributes_for裏面還是在TS內部? accept_nested_attributes_for通常自行工作 - 我的意思是在沒有安裝TS的計算機上工作? – prograils

+0

需要說明的是:belongs_to關聯現在默認添加了一個驗證,這就是錯誤與之相關的內容。如果你願意,你可以禁用這種行爲:'Rails.application.config.active_record.belongs_to_required_by_default = false' – pat