这让我困扰了好几天。我有简单的设置:
class User < ActiveRecord::Base
has_many :inquiries, dependent: :destroy
end
class Inquiry < ActiveRecord::Base
belongs_to :user
has_many :entries, dependent: :destroy
end
class Entry < ActiveRecord::Base
belongs_to :inquiry
end
这个想法是保存用户提交的查询(门票),并将条目(回复)保存在具有该关系的另一个模型中。
在查询节目视图中,我有一个表单,用于提交包含查询ID的隐藏字段的控制器。
= form_for @entry do |f|
= f.label :message, t('views.message')
= f.text_area :message
= f.hidden_field :inquiry_id, :value => @inquiry.id
.buttons
= f.submit t('views.submit')
和条目控制器
def create
@entry = Entry.new(entry_params)
@inquiry = Inquiry.find(params[:entry][:inquiry_id])
@entry.inquiry = @inquiry
respond_to do |format|
if @entry.save
format.html { redirect_to @inquiry, notice: 'Entry was created succesfully' }
#format.json { render action: 'show', status: :created, location: @inquiry }
else
format.html { redirect_to @inquiry, error: 'Entry could not be created.' }
#format.json { render json: @inquiry.errors, status: :unprocessable_entity }
logger.warn "Entry could not be created: #{@entry.attributes.inspect}, is valid?: #{@entry.valid?}"
end
end
end
def entry_params
params.require(:entry).permit(:message, :inquiry_id)
end
所有参数都正确传递但无法将条目保存在数据库中。已验证?即使没有验证也没有显示错误,返回false。我在这里缺少什么?
修改
来自记录器:
Entry could not be created: {"id"=>nil, "created_at"=>nil, "updated_at"=>nil, "i
nquiry_id"=>2, "author"=>nil, "message"=>"This is a test", "employee"=>nil}, is
valid?: false
试图保存!
irb(main):023:0> @entry.save!
ActiveRecord::RecordInvalid: translation missing: es.activerecord.errors.message
s.record_invalid
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/validations.rb:57:in `save!'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/attribute_methods/dirty.rb:41:in `save!'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/transactions.rb:275:in `block in save!'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/transactions.rb:330:in `block in with_transaction_returni
ng_status'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:i
n `block in transaction'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:221:i
n `within_new_transaction'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:i
n `transaction'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/transactions.rb:209:in `transaction'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/transactions.rb:327:in `with_transaction_returning_status
'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4
.0.4/lib/active_record/transactions.rb:275:in `save!'
from (irb):23
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.4
/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.4
/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/railties-4.0.4
/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
顺便说一下,我没有进入的验证,并且允许nil fiels。我还尝试了所有填充的参数,包括手动设置ID。