如果出现验证错误,如何使模式在rails中打开

问题描述 投票:0回答:1

我有点困惑,如果它引发表单的验证错误,我该如何重新呈现引导程序模式(表单)?

下面,我包含了一些在模态首次触发时起作用的代码。

[在其他人中,我知道我应该将表格的开头更改为<%= simple_form_for (@store) do |f|%>,以使其具有动态性。但这给了我一条错误消息,说undefined method `model_name' for nil:NilClass

stores / index.html.erb

    <!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-example-modal-lg">
  Launch Test modal
</button>

<!-- Modal -->
<div class="modal fade bd-example-modal-lg" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">New Store</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <%= render "stores/form", store:@store %>
      </div>
      <div class="modal-footer">
      </div>
    </div>
  </div>
</div>

stores / _form.html.erb

<%= simple_form_for (Store.new) do |f|%>
<%= f.input :name %>
<%= f.button :submit%>

商店控制器

def new
    @store = current_user.stores.build
    authorize @store
    respond_to do |format|
      format.html
      format.js
    end
  end

  def create
    @store = current_user.stores.create(store_params)
    authorize @store
    if @store.save
      redirect_to stores_path
    else
      render action: 'new'
    end
  end
javascript jquery ruby-on-rails bootstrap-modal
1个回答
0
投票

当表单在“索引”页面上呈现时,它要求在控制器的“索引”操作中分配@store变量。

class StoresController < ApplicationController
  def index
    @store = current_user.stores.build
    # ...
  end
end

似乎没有在此处分配。显示您的“索引”操作。

© www.soinside.com 2019 - 2024. All rights reserved.