【问题标题】:param is missing or the value is empty error while rendering edit form in Rails4.x在 Rails4.x 中呈现编辑表单时参数丢失或值为空错误
【发布时间】:2014-05-19 18:04:33
【问题描述】:

大师,

我正在制作一个简单的编辑表单,但在 Rails 4.x 中出现此错误。有趣的是,edit.html.erb 基本上是 new.html.erb 的大部分副本,并且 new.html.erb 工作得很好。

错误是:

参数缺失或值为空:teacher

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

这是教师控制器:

class TeacherController

  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 

【问题讨论】:

  • 第一个代码块没有结束:cellphone,
  • zishe,这只是错误的例子。

标签: ruby-on-rails


【解决方案1】:

您必须将编辑操作拆分为 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 =&gt; { :action =&gt; 'edit'},因为它正在路由到 update 操作。如果您使用&lt;%= form_for @teacher do |f|%&gt;,这会自动发生

【讨论】:

  • Here 可以找到不同操作的一些信息。
  • 谢谢。我将使用我必须更改的详细信息更新帖子
【解决方案2】:

基本上,我不得不将控制器中的编辑操作拆分为 udpate 和 edit。我在 lynda.com 上看到了一个可行的示例并遵循了它。其他人也说了同样的话。现在它的工作。

我必须在 routes.rb 中添加 :patch 关键字

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

编辑表单是新表单的副本,只是操作是更新而不是创建,并且必须传递 id。

<%= 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多