Rails 不同模型的新动作
Rails New Action with Different Models
我在 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
尝试将您的 computer_params
重写为:
private
def computer_params
params.require(:computer).permit(:computer_name, :cpu_tag, :serial, :location, :brand, :model, :ram, :cpu, :os, :warranty, :comments)
end
您的原始代码中 params
和 require
似乎颠倒了。
希望对您有所帮助!
为了补充答案,您可以使用您的代码进行一些修复:
1 次验证
当定义相同的presence
验证时,可以pass multiple arguments(属性)到方法:
#app/models/computer.rb
class Computer < ActiveRecord::Base
validates :serial, :location, :brand, :model, :ram, :cpu, :os, :warranty, :comments, presence: true
end
2 个参数
Rails' strong params
功能非常具体,说明您需要“要求”一个顶级参数,然后“允许”其子参数:
def computer_params
params.require(:computer).permit(:computer_name,:cpu_tag, :serial,:location,:brand,:model,:ram,:cpu,:os,:warranty,:comments)
end
3 个循环
在编程中,最高效的代码获胜。
这意味着您不应该一次又一次地复制一堆代码(使用 attributes
方法):
#app/views/computers/new.html.erb
<%= form_for @computer do |f| %>
<% @computer.attributes.each do |attr| %>
<% xtra = "green" if attr == :computer_name %>
<%= content_tag :h4, class: "misform #{xtra}" do %>
<%= f.label attr.to_sym, attr.titleize + ":" %>
<%= f.text_field attr.to_sym %>
<% end %>
<% end %>
<%= f.submit 'Add The Computer'%>
<% end %>
看看它有多干净?
4 HTML 类
您使用过这两个 class 名称:
mimsformblack
mimsformgreen
看看我的 #3
推荐,你能看出这是多么低效吗?它违反了一个叫做 DRY (Don't Repeat Yourself) 的原则,在这个原则中,您应该使用尽可能少的代码来提供尽可能多的功能。
您可以将 multiple CSS classes 应用于每个元素,这意味着您将能够执行以下操作:
<div class="mimsform">This will be black</div>
<div class="mimsform green">This will be green</div>
5 创建
当您在 Rails 中 创建 时,您必须 将对象保存 到模型中:
def create
@computer = Computer.new computer_params
redirect_to computers_path if @computer.save
end
许多新手开发人员不保存他们的新对象,从而阻止他们将数据实际保存到数据库中。
我在 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
尝试将您的 computer_params
重写为:
private
def computer_params
params.require(:computer).permit(:computer_name, :cpu_tag, :serial, :location, :brand, :model, :ram, :cpu, :os, :warranty, :comments)
end
您的原始代码中 params
和 require
似乎颠倒了。
希望对您有所帮助!
为了补充答案,您可以使用您的代码进行一些修复:
1 次验证
当定义相同的presence
验证时,可以pass multiple arguments(属性)到方法:
#app/models/computer.rb
class Computer < ActiveRecord::Base
validates :serial, :location, :brand, :model, :ram, :cpu, :os, :warranty, :comments, presence: true
end
2 个参数
Rails' strong params
功能非常具体,说明您需要“要求”一个顶级参数,然后“允许”其子参数:
def computer_params
params.require(:computer).permit(:computer_name,:cpu_tag, :serial,:location,:brand,:model,:ram,:cpu,:os,:warranty,:comments)
end
3 个循环
在编程中,最高效的代码获胜。
这意味着您不应该一次又一次地复制一堆代码(使用 attributes
方法):
#app/views/computers/new.html.erb
<%= form_for @computer do |f| %>
<% @computer.attributes.each do |attr| %>
<% xtra = "green" if attr == :computer_name %>
<%= content_tag :h4, class: "misform #{xtra}" do %>
<%= f.label attr.to_sym, attr.titleize + ":" %>
<%= f.text_field attr.to_sym %>
<% end %>
<% end %>
<%= f.submit 'Add The Computer'%>
<% end %>
看看它有多干净?
4 HTML 类
您使用过这两个 class 名称:
mimsformblack
mimsformgreen
看看我的 #3
推荐,你能看出这是多么低效吗?它违反了一个叫做 DRY (Don't Repeat Yourself) 的原则,在这个原则中,您应该使用尽可能少的代码来提供尽可能多的功能。
您可以将 multiple CSS classes 应用于每个元素,这意味着您将能够执行以下操作:
<div class="mimsform">This will be black</div>
<div class="mimsform green">This will be green</div>
5 创建
当您在 Rails 中 创建 时,您必须 将对象保存 到模型中:
def create
@computer = Computer.new computer_params
redirect_to computers_path if @computer.save
end
许多新手开发人员不保存他们的新对象,从而阻止他们将数据实际保存到数据库中。