每当我尝试使用提交按钮将表单中的数据保存到数据库中时,我都会遇到此错误,有人能解释一下为什么会发生这种情况,原因是什么吗?我是RoR的新手,还在学习:“)
undefined method `type' for #<Order id: nil, paper_size: "A4", color: "Black & White", quantity: 16, paper_style: "Black to Back", description: "asdasdasdasd", created_at: nil, updated_at: nil, first_name: "test", last_name: "example", phone_number: 123456789, email: "example@test.com">
订单控制器.rb
class OrdersController < ApplicationController
# GET to /orders/new
def new
@order = Order.new
end
# POST to /orders
def create
@order = Order.create(order_params)
if @order.save
redirect_to root_path
else
redirect_to about_path
end
end
private
def order_params
params.require(:order).permit(:first_name, :last_name, :phone_number, :email, :paper_size, :color, :paper_style, :quantity, :description, files: [] )
end
end
查看/订单/新.html.erb
<div class="container">
<h1 class="text-center">Order From Home!</h1>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<%= form_for @order do |f| %>
<%= f.label :first_name%>
<%= f.text_field :first_name, class:"form-control" %><br/>
<%= f.label :last_name %>
<%= f.text_field :last_name, class:"form-control" %><br/>
<%= f.label :phone_number %>
<%= f.text_field :phone_number, class:"form-control" %><br/>
<%= f.label :email %>
<%= f.text_field :email, class:"form-control" %><br/>
<%= f.label :files %>
<%= f.file_field :files, multiple: true %><br/>
<%= f.label :paper_size %>
<%= f.select :paper_size, ['A4', 'B4'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :color %>
<%= f.select :color, ['Black & White', 'Color'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :paper_style %>
<%= f.select :paper_style, ['Black to Back', 'Side to Side'], { prompt: 'Select' }, class:'form-select' %><br/>
<%= f.label :quantity %>
<%= f.select :quantity, options_for_select(0..500), { prompt: "Select" }, class:'form-select' %><br/>
<%= f.label :description %>
<%= f.text_area :description, class:"form-control" %><br/>
<div class="btn-order">
<%= f.submit %>
</div>
<% end %>
</div>
</div>
</div>
schema.rb(订单表)
create_table "orders", force: :cascade do |t|
t.string "paper_size"
t.string "color"
t.integer "quantity"
t.string "paper_style"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.integer "phone_number"
t.string "email"
end
为什么会发生这种情况?@order.create和@ www.example.com之间有什么区别order.new??
UPDATEmy order.rb(model)对:type
进行了验证,我听人说,在Rails中创建表时,应该避免使用type
和date
class Order < ApplicationRecord
belongs_to :admin
validates :paper_size, presence: true
validates :color, presence: true
validates :quantity, presence: true
validates :type, presence: true
validates :first_name, presence: true
validates :last_name, presence: true
validates :phone_number, presence: true
validates :email, presence: true
has_many_attached :files
end
我通过迁移更改了db表中的列名,并将模型文件中的新列名替换为
class Order < ApplicationRecord
belongs_to :admin
validates :paper_size, presence: true
validates :color, presence: true
validates :quantity, presence: true
validates :paper_style, presence: true
validates :first_name, presence: true
validates :last_name, presence: true
validates :phone_number, presence: true
validates :email, presence: true
has_many_attached :files
end
1条答案
按热度按时间u5rb5r591#
试试这个
表单完成后,将调用创建操作
Order.create
将直接创建一个对象,并将参数传递到而不是赋值。此外,如果其中有任何错误,Order.create!(order_params)
将引发错误。您可以在控制台中使用Order.create!(order_params)查找错误。
解决方案
:删除order.rb中的验证名称“type”