当RoR验证失败时,保存textarea表单字段

时间:2015-04-23 17:49:07

标签: html ruby-on-rails ruby validation

我在验证失败时尝试重新填充表单的字段,但是textarea字段没有重新填充。

这是我的表单( new.html.erb ):

<%= form_for @testimonial, html: {multipart: true} do |f|  %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :name %>
  <%= f.text_field :name, class: 'form-control' %>

  <%= f.label :title %>
  <%= f.text_field :title, class: 'form-control' %>

  <%= f.label :company %>
  <%= f.text_field :company, class: 'form-control' %>

  <%= f.label :comment %>
  <%= f.text_area :comment, rows: 10, class: 'form-control'%>

  <%= f.label :avatar %>
  <%= f.file_field :avatar, class: 'form-control' %>
  <%= f.hidden_field :avatar_cache %>

  <%= image_tag(@testimonial.avatar_url) if @testimonial.avatar? %>
  <br/>
  <%= f.submit "Create Testimonial", class: "btn btn-primary" %>
<% end %>

这是控制器:( testimonials_controller.rb

def new
    @testimonial = Testimonial.new
    session[:name] = params[:name]
    session[:title] = params[:title]
    session[:company] = params[:company]
    session[:comment] = params[:comment]
end

def create
    @testimonial = Testimonial.new(testimonial_params)
    if @testimonial.save
      flash[:success] = "Testimonial successfully added!"
      redirect_to testimonials_url
    else
      render 'new'
    end
end

当表单验证失败时,其他所有内容都会填充,而不是textarea(注释)。您将在底部的调试输出中注意到params [:comment]正在会话中传递。

以下是截图: enter image description here

那么,是否有不同的方法来填充textarea字段以使其工作?

1 个答案:

答案 0 :(得分:1)

您应该检查控制器中的允许字段:testemonial_parmas,检查是否包含comment