oobie11
oobie11

Reputation: 925

Why am I getting argument out of range exception

Im a rails beginner on Rails 4. Im trying to create a wine list that takes the name of the winery and the year of the bottle. Here is my form

<%= form_for @wine do |f| %>
    <%# render "errors", object: @wine %>
    <div class="form-group input-group input-group-lg">
        <%= f.text_field :name, placeholder: "Enter the winery name", class: "form-control input-lg" %>
    </div>
    <div class="form-group input-group input-group-lg">
        <%= select_year(Date.today, start_year: Time.now.year, end_year: Time.now.year - 90, field_name: :year, prefix: :wine) %>
    </div>
    <div class="form_group input-group">
        <%= f.submit "Add wine", class: "btn btn-success" %>
    </div>
<% end %>

Here is my controller

class WinesController < ApplicationController
  before_action :set_wine, only: [:show, :edit, :update, :destroy]

  def index
    @wines = Wine.all
  end

  def new
    @wine = Wine.new
  end

  def create
    @wine = Wine.new(wine_params)
    if @wine.save
        flash[:notice] = "Successfully created..."
        redirect_to @wine
    else
        flash.now[:error] = "There was a problem"   
        render "new"
    end
  end

  def show
  end

  def edit
  end

  def update
    if @wine.update(wine_params)
        redirect_to @wine
    else
        flash[:error] = "Something went wrong"
        render "edit"
    end
  end

  def destroy
    @wine.destroy
    redirect_to wines_path
  end

  private

    def set_wine
        @wine = Wine.find(params[:id])
    end

    def wine_params
        params.require(:wine).permit(:name, :year)
    end
end

I have my wines table with a name:string column and a year:datetime column

Whenever I try and create a new wine I get an argument out of range exception and it highlights @wine = Wine.new(wine_params) in my create action. What am I doing wrong?

My log

Started GET "/wines/new" for 127.0.0.1 at 2013-09-04 10:55:54 -0700
Processing by WinesController#new as HTML
  Rendered wines/_form.html.erb (2.0ms)
  Rendered wines/new.html.erb within layouts/application (2.6ms)
  Rendered layouts/_header.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 10ms (Views: 9.3ms | ActiveRecord: 0.0ms)


Started GET "/assets/comingsoonbg.png" for 127.0.0.1 at 2013-09-04 10:55:54 -0700


Started GET "/wines/new" for 127.0.0.1 at 2013-09-04 10:55:54 -0700
Processing by WinesController#new as HTML
  Rendered wines/_form.html.erb (2.1ms)
  Rendered wines/new.html.erb within layouts/application (2.7ms)
  Rendered layouts/_header.html.erb (0.2ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 10ms (Views: 9.8ms | ActiveRecord: 0.0ms)


Started POST "/wines" for 127.0.0.1 at 2013-09-04 10:55:59 -0700
Processing by WinesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"FCd3wvCov+mugnJuzwplKD/eVskJKgxweh1mK0pG2wM=", "wine"=>{"name"=>"kjkljhk", "year"=>"2013"}, "commit"=>"Add wine"}
Completed 500 Internal Server Error in 1ms

ArgumentError (argument out of range):
  app/controllers/wines_controller.rb:13:in `create'


  Rendered /Users/me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.7ms)
  Rendered /Users/me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.9ms)
  Rendered /Users/me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.9ms)
  Rendered /Users/me/.rvm/gems/ruby-2.0.0-p247/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (16.2ms)

My schema for wines in schema.rb

create_table "wines", force: true do |t|
    t.datetime "year"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
  end

Upvotes: 5

Views: 14590

Answers (1)

Fred
Fred

Reputation: 8602

Date and Time always require me to think twice! In the log, params contains a string with the year from the select_year. The simplest solution was to just store the integer representation, if all you'll need are years. If you later change to select_date, you'll need to build a Date object in your controller by extracting the parts from the params hash. See Form Helpers Guide for more detail... well, here is just a bit.

In the view:

<%= select_date Date.today, prefix: :start_date %>

In the controller, to make your Date object:

Date.civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)

ActiveRecord expects an attribute to have one entry in the params hash, but Date objects require multiple values. The date helper date_select (not select_date) will pass back a hash that Rails will convert, with multiparameter assignment, to a Date during mass assignment.

<%= date_select :person, :birth_date %>

If you used date_select, you's see something like this in the params hash:

{:person => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', 'birth_date(3i)' => '22'}}

Notice the keys in the :person hash describe the order and type of the multipart object so that Rails can figure out how to create the attribute (for example, the hash key birth_date(2i) is the second component of the birth_date attribute in a Person model object and is an integer).

Upvotes: 7

Related Questions