【发布时间】:2016-01-06 21:56:53
【问题描述】:
我在 Rails 中遇到了创建操作的问题 - 我的控制器中有以下信息:
ComputerController
def create
@computer = Computer.new(computer_params)
redirect_to computers_path
end
private
def computer_params
require.params(:computer).permit(:computer_name,
:cpu_tag,:serial,:location,:brand,:model,:ram,:cpu,:os,:warranty,:comments)
end
然后在我的模型中我有一些验证:
class Computer < ActiveRecord::Base
validates :computer_name, uniqueness: true, presence: true,
length:{maximum: 12}
validates :cpu_tag, length: {maximum: 4}, uniqueness: true,
:numericality => {:only_integer => true}
validates :serial, presence: true
validates :location, presence: true
validates :brand, presence: true
validates :model, presence: true
validates :ram, presence: true
validates :cpu, presence: true
validates :os, presence: true
validates :warranty, presence: true
validates :comments, presence: true
end
视图new.html.erb是:
<div class="row text-center">
<h2 class = "mimsinfoblackindex">Add A Computer To The Inventory </h2><hr/>
<div class="col-md-3 description_pc text-left">
<%= form_for @computer do |f|%>
<h4 class = "mimsformgreen">
<%= f.label :computer_name,'Computer Name:'%>
<%= f.text_field :computer_name%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :cpu_tag, 'Computer Tag:'%>
<%= f.text_field :cpu_tag%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :serial, 'Serial:'%>
<%= f.text_field :serial%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :location, 'Location:'%>
<%= f.text_field :location%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :brand, 'Brand:'%>
<%= f.text_field :brand%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :model, 'Model:'%>
<%= f.text_field :model%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :ram, 'Ram:'%>
<%= f.text_field :ram%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :cpu, 'Processor:'%>
<%= f.text_field :cpu %>
</h4>
<h4 class = "mimsformblack">
<%= f.label :os, 'Operating System:'%>
<%= f.text_field :os%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :warranty, 'Warranty:'%>
<%= f.text_field :warranty%>
</h4>
<h4 class = "mimsformblack">
<%= f.label :comments, 'Comments:'%>
<%= f.text_field :comments%>
</h4>
<%= f.submit 'Add The Computer'%>
<% end %>
我已经为我的模型进行了 TDD,我没有任何问题,但是当我提交计算机表单时,我在屏幕上收到一条错误消息:
wrong number of arguments (0 for 1)
private
def computer_params
require.params(:computer).permit(:computer_name,:cpu_tag,
:serial,:location,:brand,:model,:ram,:cpu,:os,:warranty,:comments)
end
【问题讨论】:
标签: ruby-on-rails ruby strong-parameters