Carlos Morales
Carlos Morales

Reputation: 1149

Issue getting ActiveRecord::StatementInvalid on new record

I'm tryint to save information but seems to be hard.

Here my controller /app/controllers/finance_management/voucher_controller.rb

class FinanceManagement::VoucherController < ActionController::Base

  def new  
    @voucher = Voucher.new
  end

  def create
    Voucher.create(params[:voucher])
  end

  def voucher_params
    params.require(:voucher).permit(:voucher_num)
  end

end

Here is my model /app/models/voucher.rb

class Voucher < ActiveRecord::Base
end

Here is my view /app/finance_management/voucher/new.html.erb

<%= form_for :obj_voucher, :url => { :controller => "finance_management/voucher", :action => "create" }  do |f| %>
Number<%= f.text_field :voucher_num %>
      <%= f.submit :submit %>
<% end %>

Here my routes.rb

Rails.application.routes.draw do

  namespace :finance_management do
    resources :voucher 
  end

  match ':controller(/:action(/:id(.:format)))', via: [:get, :post]
end

Here my logs

Started POST "/finance_management/voucher" for 127.0.0.1 at 2016-01-17 21:00:39 -0500
Processing by FinanceManagement::VoucherController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"222222=", "voucher"=>{"voucher_num"=>"1111"}, "commit"=>"submit"}
 (0.1ms)  BEGIN
 SQL (1.5ms)  INSERT INTO `vouchers` (`created_at`, `updated_at`) VALUES ('2016-01-18 02:00:39', '2016-01-18 02:00:39')
 Mysql2::Error: Field 'voucher_num' doesn't have a default value:  INSERT INTO `vouchers` (`created_at`, `updated_at`) VALUES ('2016-01-18 02:00:39', '2016-01-18 02:00:39')
 (0.2ms)  ROLLBACK
 Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.8ms)

 ActiveRecord::StatementInvalid (Mysql2::Error: Field 'voucher_num' doesn't have a default value: INSERT INTO `vouchers` (`created_at`, `updated_at`) VALUES ('2016-01-18 02:00:39', '2016-01-18 02:00:39')):

I tried several ways but cannot save information but got errors:

<%= form_for :user, :url => {:controller=>"finance_management/voucher",:action=>'create'} do |f|%>

<%= form_for @user, :url => {:controller=>"finance_management/voucher",:action=>'create'} do |f|%>

Also changed this:

def create  
  @voucher= Voucher.create(params[:voucher_params])
end

def voucher_params
  params.require(:voucher).permit(:voucher_num)
end

The field is not saving

Mysql2::Error: Field 'voucher_num' doesn't have a default value:   INSERT INTO `vouchers` (`created_at`, `updated_at`) VALUES ('2016-01-18 02:00:39', '2016-01-18 02:00:39')

Upvotes: 0

Views: 115

Answers (3)

Richard Peck
Richard Peck

Reputation: 76774

For benefit of the doubt, here's how the code should be structured:

#config/routes.rb
namespace :finance_management do
   resources :vouchers
end

#app/controllers/financial_management/vouchers_controller.rb
class FinanceManagement::VouchersController < ActionController::Base

  def new  
    @voucher = Voucher.new
  end

  def create
    @voucher = Voucher.new voucher_params
    @voucher.save
  end

  private

  def voucher_params
    params.require(:voucher).permit(:voucher_num)
  end
end

#app/views/finance_management/vouchers/new.html.erb
<%= form_for [:financial_management, @voucher] do |f| %>
     <%= f.text_field :voucher_num, placeholder: "Number" %>
     <%= f.submit %>
<% end %>

Upvotes: 0

Yorkshireman
Yorkshireman

Reputation: 2343

Try:

  def create
    Voucher.create(voucher_params)
  end

Upvotes: 1

Igor Belo
Igor Belo

Reputation: 738

ActionController params can't be used in ActiveModel mass assignment directly, you have to use strong parameters instead.

Upvotes: 1

Related Questions