我正在使用 rails 4.0 我想知道如何使用 activemodel 验证 date_select
假设我的代码如下
应用程序/模型/book.rb
class Book
include ActiveModel::Model
include ActiveModel::Validations
attr_accessor :title, :written_on
validates :title, presence: true, allow_blank: false
# How validates :written_on if I use date_select Please look in my view below
end
应用程序/控制器/books.rb
class BooksController < ApplicationController
#...
def new
@book = Book.new
end
def create
@book = Book.new(book_params)
if @book.valid?
#Do something
end
end
#...
end
app/views/books/new.html.erb
<% form_for @book do |f| %>
...
<%= f.date_select :written_on %>
...
<% end %>
我也尝试添加
attr_accessor 'written_on(1i)'
到我的书模型,但我收到错误无效属性名称'written_on(1i)'
非常感谢这里的帮助。