私は Ruby on Rails に比較的collection_select
慣れておらず、新しいフォームを使用して正常に送信するのに問題があります。私のコードの背景:
- User モデルと Payment モデルがあります。ユーザー
has_many
支払い、および支払いbelongs_to
ユーザー。 - ユーザーのログインを管理するためにDeviseを使用しています。ユーザーがログインすると、フォームを介して新しい支払いを追加できます。
collection_select
フォームから選択できる現在のユーザーに関連付けられた電子メールアドレスのリストを生成するために使用しています。
私は 2 つの問題を抱えていますが、どちらも collection_select の誤った使用法に起因すると考えられます。
フォームで電子メール アドレスを選択して送信すると、空白ではないエントリを送信した場合でも、電子メール アドレスを空白にすることはできないというエラー メッセージが表示されます。エラー メッセージは、支払いモデルでの検証の結果です。
validates :email, :presence => true
:email
Payment モデルの検証行を一時的にコメントアウトしたところ、次のエラー メッセージが表示されSQLite3::SQLException: cannot start a transaction within a transaction: begin transaction
ました。
私は、運がなければ何が間違っているのかを理解しようとしばらく費やしました。Stackoverflow の他の投稿では、この問題を見つけることができませんでした。これは単純な間違いだと思いますが、理解できないようです。私が間違っていることを誰かが知っていますか?
/app/models/payment.rb
require 'valid_email'
class Payment < ActiveRecord::Base
include ActiveModel::Validations
attr_accessible :amount, :description, :email, :frequency, :paid, :user_id
belongs_to :user
#validates :email, :presence => true, :email => true (commented out as described above)
validates :amount, :presence => true, :format => { :with => /^\d+??(?:\.\d{0,2})?$/ }, :numericality => {:greater_than => 0, :less_than => 100000}
validates :description, :presence => true
end
/app/models/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :payments
end
/app/controllers/payments_controller.rb (作成して新規作成):
class PaymentsController < ApplicationController
before_filter :authenticate_user!
def create
@payment = current_user.payments.new(params[:payment])
@payment_current_user = current_user.payments.all
respond_to do |format|
if @payment.save
format.html { redirect_to payments_url, :flash => { notice: 'Payment was successfully created.' } }
format.json { render json: @payment, status: :created, location: @payment }
else
format.html { render action: "new" }
format.json { render json: @payment.errors, status: :unprocessable_entity }
end
end
end
def new
@payment = current_user.payments.new
# get list of current_user's email addresses in form
@payment_current_user = current_user.payments.all
respond_to do |format|
format.html # new.html.erb
format.json { render json: @payment }
end
end
end
/app/views/payments/_form.html.erb
<%= form_for(@payment) do |f| %>
<form class="form-horizontal">
<div class="field">
<div class="control-group">
<%= f.label :email, "Lendee's Email", class: "control-label" %><br />
<div class="controls">
<%= collection_select :payment, :user_id, @payment_current_user, :email, :email, {:include_blank => "Please select"} %>
</div>
</div>
</div>
<div class="control-group">
<div class="actions">
<div class="controls">
<%= f.submit %>
</div>
</div>
</div>
</form>
<% end %>
[送信] をクリックした後に表示される完全なエラー メッセージ:
ActiveRecord::StatementInvalid in PaymentsController#create
SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction
エラーに関連するアプリケーション トレース:
app/controllers/payments_controller.rb:52:in
block in create' app/controllers/payments_controller.rb:51:in
create'エラーに関連するリクエスト パラメータ:
{"utf8"=>"✓", "authenticity_token"=>"K8+NnWHIIxVfcC5IvhLhoSKOzuScFrpnHOPTt1pVdpA=", "支払い"=>{"user_id"=>"new@gmail.com", "金額"=>"d", "frequency"=>"隔週", "description"=>"adf", "paid"=>"Pending"}, "commit"=>"Create Payment"}