2014-05-19 16 views
2

大師,參數是丟失或爲空值誤差在渲染編輯形式Rails4.x

我想提出一個簡單的編輯形式和我得到的Rails的4.x的這個錯誤有趣的是,edit.html.erb基本上是new.html.erb的一個副本,而new.html.erb工作得很好。

的錯誤是:

參數是丟失或爲空值:老師

def teacher_params 
    params.require(:teacher).permit(:firstname, 
            :lastname, 
            :email, 
            :cellphone, 

應用程序跟蹤是:

app/controllers/teacher_controller.rb:39:in `teacher_params' 
app/controllers/teacher_controller.rb:23:in `edit' 

編輯的形式是:

<h1>Add A teacher</h1> 

<%= form_for @teacher, :url => { :action => 'edit'}, :id => @teacher.id, :html => { :multipart => true } do |f| %> 
<table summary="teacher form fields"> 
    <tr> 
    <th>First Name*</th> 
    <td><%= f.text_field :firstname %></td> 
    </tr> 
    <tr> 
    <th>Last Name*</th> 
    <td><%= f.text_field :lastname %></td> 
    </tr> 
    <tr> 
    <th>Email*</th> 
    <td><%= f.email_field :email %></td> 
    </tr> 
    <tr> 
    <th>Cellphone</th> 
    <td><%= f.telephone_field :cellphone %></td> 
    </tr> 
    <tr> 
    <th>Username*</th> 
    <td><%= f.text_field :username %></td> 
    </tr> 
    <tr> 
    <tr> 
    <th>Password*</th> 
    <td><%= f.password_field :password %></td> 
    </tr> 
    <tr> 
    <th>Confirm Password*</th> 
    <td><%= f.password_field :password_confirmation %></td> 
    </tr> 
    <tr> 
    <th>Address Street#</th> 
    <td><%= f.text_field :addr_streetno %></td> 
    <th>Apt #</th> 
    <td><%= f.number_field :addr_aptno %></td> 
    </tr> 
    <tr> 
    <th>City</th> 
    <td><%= f.text_field :addr_city %></td> 
    <th>State</th> 
    <td><%= f.text_field :addr_state %></td> 
    <th>Zip</th> 
    <td><%= f.number_field :addr_zip %></td> 
    </tr> 
    <tr> 
    <th>Photo</th> 
    <td><%= f.file_field :photo %></td> 
    </tr> 
</table> 

    <%= f.submit 'Update teacher' %> 
<% end %> 

<% if @teacher.errors.any? %> 
    <ul class="Signup_Errors"> 
    <% for message_error in @teacher.errors.full_messages %> 
     <li>* <%= message_error %></li> 
    <% end %> 
    </ul> 
    <% end %> 
</div> 

莫德爾是:

class Teacher < ActiveRecord::Base 

has_many :students 

has_secure_password 

EMAIL_REGEX = /\A[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}\z/i 
CELLPHONE_REGEX = /\A([0-9](|-)?)?(\(?[0-9]{3}\)?|[0-9]{3})(|-)?([0-9]{3}(|-)?[0-9]{4}|[a-zA-Z0-9]{7})\z/i 
validates :firstname, :presence => true 
validates :lastname, :presence => true 
validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 } 
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX 
validates :cellphone, :presence => true, :format => CELLPHONE_REGEX 
validates :addr_aptno, numericality: { only_integer: true, :greater_than => 0 } 
validates :addr_zip, numericality: { only_integer: true, :greater_than => 0 } 
end 

這裏是老師控制器:

類TeacherController < ApplicationController中 DEF索引 @教師= Teacher.all 端

def new 
     @teacher = Teacher.new 
    end 

    def create 
     @teacher = Teacher.new(teacher_params) 
     if @teacher.save 
     flash[:notice] = "Teacher created." 
     redirect_to :action => 'index' 
     else 
     render :action => 'new' 
     end 
    end 

    def edit 
    @teacher = Teacher.find(params[:id]) 
    # Update the object 
    if @teacher.update_attributes(teacher_params) 
     # If update succeeds, redirect to the list action 
     flash[:notice] = "Teacher updated." 
     redirect_to :action => 'index' 
    else 
     # If save fails, redisplay the form so user can fix problems 
     render :action => 'edit' 
    end 
    end 

    def show 
    end 

private 

    def teacher_params 
     params.require(:teacher).permit(:firstname, 
             :lastname, 
             :email, 
             :cellphone, 
             :username, 
             :password, 
             :password_confirmation, 
             :addr_streetno, 
             :addr_aptno, 
             :addr_city, 
             :addr_state, 
             :addr_zip, 
             :photo) 
    end 
end 

這裏是遷移文件:

create_table :teachers do |t| 

    t.string :firstname, null: false 
    t.string :lastname, null: false 
    t.string :email, null: false 
    t.string :cellphone 
    t.string :username, null: false 
    t.string :password_digest, null: false 
    t.string :addr_streetno 
    t.integer :addr_aptno 
    t.string :addr_city 
    t.string :addr_state 
    t.integer :addr_zip 
    t.binary :photo, :limit => 0.5.megabyte 

    t.timestamps 
end 
+0

第一碼塊沒有被終止:'手機,' – zishe

+1

zishe,即錯誤的只是例子。 – gvermag

回答

2

你必須編輯動作分成2個行動,因爲編輯是默認的只是一個get要求:

def edit 
    @teacher = Teacher.find(params[:id]) 
    respond_to { |format| format.html } 
end 

def update 
    @teacher = Teacher.find(params[:id]) 
    if @teacher.update_attributes(teacher_params) 
    ... 
end 

而且從表單中刪除:url => { :action => 'edit'},因爲它的路由到update行動。這種情況會自動發生,如果你使用<%= form_for @teacher do |f|%>

+0

[這裏](http://www.tutorialspoint.com/ruby-on-rails/rails-controllers.htm)可以找到不同動作的一些信息。 –

+0

謝謝。我將更新帖子並提供必須更改的詳細信息 – gvermag

0

本質上,我不得不將控制器中的編輯動作分成udpate和編輯。我在lynda.com上看到了一個工作示例並遵循它。其他人也說過同樣的事情。現在它的工作。

我不得不添加的:在routes.rb中

match ':controller(/:action/(:id))', :via => [:get, :post, :patch] 

編輯表單補丁關鍵字是新形式的翻版,只是行動是更新而不是創建和標識必須通過。

<%= form_for @teacher, :url => { :action => 'update', :id => @teacher.id}, :html => { :multipart => true } do |f| %> 

這是在控制器中的新的代碼:

 def edit 
    @teacher = Teacher.find(params[:id]) 
    end 

    def update 
    @teacher = Teacher.find(params[:id]) 
    # Update the object 
    if @teacher.update_attributes(teacher_params) 
     # If update succeeds, redirect to the list action 
     flash[:notice] = "Teacher updated." 
     redirect_to :action => 'show', :id => @teacher.id 
    else 
     # If update fails, redisplay the form so user can fix problems 
     render :action => 'edit' 
    end 
    end 
相關問題