Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing collection_select and getting an unexpected error from sqlite3 - anyone know why?
    primarykey
    data
    text
    <p>I am relatively new to Ruby on Rails and am having an issue using <code>collection_select</code> and getting my new form to submit successfully. Some background on my code:</p> <ul> <li>I have a User model and a Payment model; a User <code>has_many</code> payments, and a Payment <code>belongs_to</code> user.</li> <li>I am using <em>Devise</em> to manage user logins, and when a user is logged in they can add a new payment via a form.</li> <li>I am using <code>collection_select</code> to generate a list of email addresses associated with the current user that can be selected via the form.</li> </ul> <p>I am having two issues, which I believe both stem from my incorrect usage of collection_select:</p> <ol> <li><p>When I select an email address on the form and submit, I get an error message that the email address can't be blank, even when I have submitted a non-blank entry. The error message is a result of validation in my Payment model, <code>validates :email, :presence =&gt; true</code></p></li> <li><p>I temporarily commented out the <code>:email</code> validation line in my Payment model, and now am getting the error message: <code>SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction</code>.</p></li> </ol> <p>I have spent awhile trying to figure out what I am doing wrong without luck; I was not able to find this issue in other posts on Stackoverflow. I am guessing this is a simple mistake but can't seem to figure it out. Does anyone know what I'm doing wrong?</p> <p>/app/models/payment.rb</p> <pre><code>require 'valid_email' class Payment &lt; ActiveRecord::Base include ActiveModel::Validations attr_accessible :amount, :description, :email, :frequency, :paid, :user_id belongs_to :user #validates :email, :presence =&gt; true, :email =&gt; true (commented out as described above) validates :amount, :presence =&gt; true, :format =&gt; { :with =&gt; /^\d+??(?:\.\d{0,2})?$/ }, :numericality =&gt; {:greater_than =&gt; 0, :less_than =&gt; 100000} validates :description, :presence =&gt; true end </code></pre> <p>/app/models/user.rb</p> <pre><code>class User &lt; ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me has_many :payments end </code></pre> <p>/app/controllers/payments_controller.rb (just create and new):</p> <pre><code>class PaymentsController &lt; 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 =&gt; { 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 </code></pre> <p>/app/views/payments/_form.html.erb</p> <pre><code>&lt;%= form_for(@payment) do |f| %&gt; &lt;form class="form-horizontal"&gt; &lt;div class="field"&gt; &lt;div class="control-group"&gt; &lt;%= f.label :email, "Lendee's Email", class: "control-label" %&gt;&lt;br /&gt; &lt;div class="controls"&gt; &lt;%= collection_select :payment, :user_id, @payment_current_user, :email, :email, {:include_blank =&gt; "Please select"} %&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="control-group"&gt; &lt;div class="actions"&gt; &lt;div class="controls"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;% end %&gt; </code></pre> <p><strong>Full error message, which comes up after I click Submit:</strong></p> <pre><code>ActiveRecord::StatementInvalid in PaymentsController#create SQLite3::SQLException: cannot start a transaction within a transaction: begin transaction </code></pre> <ul> <li><p><strong>Application trace associated with error:</strong></p> <p>app/controllers/payments_controller.rb:52:in <code>block in create' app/controllers/payments_controller.rb:51:in</code>create'</p></li> <li><p><strong>Request parameters associated with error:</strong></p> <p>{"utf8"=>"✓", "authenticity_token"=>"K8+NnWHIIxVfcC5IvhLhoSKOzuScFrpnHOPTt1pVdpA=", "payment"=>{"user_id"=>"new@gmail.com", "amount"=>"d", "frequency"=>"biweekly", "description"=>"adf", "paid"=>"Pending"}, "commit"=>"Create Payment"}</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload