2015-10-09 26 views
1

我有一個問題,在與Rails的創建操作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 

回答

0

要添加到答案,也有一些修正,你可以讓你的代碼:


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 PARAMS

Rails的strong params功能是說明你需要 「規定」 的頂級PARAM非常具體,然後在 「許可證」 其子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類

您已經使用這兩個類名:

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 

許多新手開發人員不保存他們的新對象,阻止他們實際上將數據保存到數據庫。

+1

嗨豐富非常感謝您的幫助,我是新的在ROR中,所以我非常感謝所有對我的代碼的建議,特別是那種更好的開發人員的建議,謝謝! –

3

嘗試重寫你的computer_params到:

private 

def computer_params 
    params.require(:computer).permit(:computer_name, :cpu_tag, :serial, :location, :brand, :model, :ram, :cpu, :os, :warranty, :comments) 
end 

看來,paramsrequire在你原來的代碼是相反的。

希望它有幫助!

+0

謝謝卓然,那是錯誤..對不起菜鳥錯誤我是新的ROR,我不注意代碼的那部分... –