ruby-on-rails - 验证失败时如何重新填充表单字段?

标签 ruby-on-rails ruby ruby-on-rails-3.2

这是 erb 模板:

<div id='recipe-form'>
  <% if @recipe.errors %>
    <div id='errors'>
      <% @recipe.errors.messages.each do |field, messages| %>
        <div class='error'>
          <div class=field'><%= field %></div>
          <div class='messages'>
            <ul>
            <% messages.each do |message| %>
              <li><%= message %></li>
            <% end %>
            </ul>
          </div>
        </div>
      <% end %>
    </div>
  <% end %>
  <%= form_for @recipe, :html => {:multipart => true}, :url => '/recipes'  do |f| %>

    <%= f.label :title, 'title' %>
    <%= f.text_field :title %>

    <div id="photo-upload">
      <%= file_field :photo0, :image, :id => 0 %>
    </div>

    <div id='existing-photos'>
      <% recipe.photos.each do |photo| %>
        <div id='<%= photo.id %>'>
          <img src='<%= photo.image.url(:thumb) %>' />
          <ul>
            <li>
              <%= link_to 'delete',
                    recipe_photo_url(
                      :recipe_id => @recipe.slug,
                      :id => photo.id
                    ),
                    :method => :delete,
                    :remote => true
              %>
            </li>
          </ul>
        </div>
      <% end %>
    </div>

    <%= f.label :body, 'body' %>
    <%= f.cktext_area :body, :ckeditor => {:width => "500"} %>

    <%= f.label :tags, 'tags (comma separated)' %>
    <%= text_field_tag :tags %>

    <%= submit_tag 'submit' %>
  <% end %>
</div>

这是创建操作:

def create
  @recipe = Recipe.new(params[:recipe])

  photo_keys = params.keys.select{|k|k.match(/^photo/)}
  @photos = []
  photo_keys.each do |photo_key|
    @photos << Photo.new(params[photo_key])
  end

  @recipe.tags = Tag.parse(params[:tags])

  @recipe.author = current_user

  if @recipe.save &&
       @photos.all?{|photo|photo.save}
    @photos.each do |photo|
      photo.recipe_id = @recipe.id
      photo.save
    end
    flash[:notice] = 'Recipe was successfully created.'
    redirect_to recipe_url(@recipe.slug)
  else
    flash[:error] = 'Could not create recipe. '
    flash[:error] += 'Please correct any mistakes below.'
    render :action => :new
  end
end

这是新操作:

def new
  @recipe = Recipe.new
end

我读到,如果我像上面那样使用 form_for,字段将自动重新填充。

当我从 erb 模板中检查 @recipe.errors 时,我可以看到 create 生成的错误在 new 时也可用。 code> 操作已呈现,但字段不会重新填充。

最佳答案

我实际上不确定 render action: 的作用,但我所做和工作的是:不渲染操作,只需使用 render :new 渲染模板.

您需要设置相同的实例变量(带有 @ 的变量),这些变量已在创建操作中。

关于ruby-on-rails - 验证失败时如何重新填充表单字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17415874/

相关文章:

ruby - 将字符串转换为哈希符号的最佳方法

ruby-on-rails - 在一个 Controller 操作中处理两个不相关的 Rails 模型的表单

javascript - 在生产环境中加载 ActiveAdmin Assets

ruby-on-rails - gem 安装在哪里?

ruby-on-rails - Rails - 我机器上的不同版本

jquery - 如何将 json 响应从 Controller 发送到 jQuery 并检查它?

javascript - 如何为元素分配 HTML id 以及如何将 JavaScript 应用于它们?

ruby-on-rails - Rails 仪表板设计 : one controller action per div

ruby - 如何在 Ruby 中进行就地放置

ruby-on-rails - 请向我推荐一些需要编写文档/测试的 rails/ruby 开源代码