4

我是 Rails 4 的 Rails 初学者。我正在尝试创建一个包含酒厂名称和瓶子年份的酒单。这是我的表格

<%= 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 %>

这是我的控制器

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

我有一个带有 name:string 列和 year:datetime 列的 wines 表

每当我尝试创造一种新葡萄酒时,我都会遇到一个argument out of range异常,它会@wine = Wine.new(wine_params)在我的创建操作中突出显示。我究竟做错了什么?

我的日志

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)

我在 schema.rb 中的葡萄酒模式

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

1 回答 1

6

日期和时间总是需要我三思而后行!在日志中,params包含一个带有年份的字符串select_year。最简单的解决方案是只存储整数表示,如果您只需要几年。如果您稍后更改为select_date,则需要通过从params哈希中提取部分来在控制器中构建一个 Date 对象。有关更多详细信息,请参阅表单助手指南......好吧,这里只是一点点。

在视图中:

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

在控制器中,使您的 Date 对象:

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

ActiveRecord 期望属性在params散列中有一个条目,但 Date 对象需要多个值。日期助手date_select(不是select_date)将传回一个哈希值,Rails 将在批量分配期间通过多参数分配将其转换为日期。

<%= date_select :person, :birth_date %>

如果您使用date_select,您会在 params 哈希中看到类似的内容:

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

注意:person散列中的键描述了多部分对象的顺序和类型,以便 Rails 可以弄清楚如何创建属性(例如,散列键birth_date(2i)是 Person 模型对象中属性的第二个组成部分,birth_date并且是一个整数) .

于 2013-09-04T20:53:23.110 回答