我想提出一個簡單的編輯形式和我得到的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
第一碼塊沒有被終止:'手機,' – zishe
zishe,即錯誤的只是例子。 – gvermag