ruby-on-rails - 在 Rails4.x 中渲染编辑表单时出现参数丢失或值为空错误

标签 ruby-on-rails

大师,

我正在制作一个简单的编辑表单,但在 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._%+-]+@[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

这是教师 Controller :

类 TeacherController < ApplicationController 定义索引 @teachers=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 

最佳答案

您必须将编辑操作分成 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|%>,则会自动发生这种情况

关于ruby-on-rails - 在 Rails4.x 中渲染编辑表单时出现参数丢失或值为空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23744341/

相关文章:

ruby-on-rails - Rails 路由中两个不同 Controller 共享路径?

css - Sass 文件不能在 IE 上运行

ruby-on-rails - require 'bundler/capistrano' 中断 capistrano 部署

ruby-on-rails - 我应该避免在 Rails View 中检查 nil 吗?

ruby-on-rails - 字符串 gsub 正则表达式中的引用匹配

ruby-on-rails - 设计不想在登录后返回 JSON

ruby-on-rails - rails ActiveRecord : Automatically Alias/Append Suffix?

ruby-on-rails - Rails 4 - simple_form 和来自 url 的预填充字段

ruby-on-rails - 我应该使用哪些选项来重新缩放视频而不会使它们在 FFMPEG Rails 中倾斜?

ruby-on-rails - Rails -- 可以在通用 rake 任务中运行迁移方法吗?