ruby-on-rails Rails:未定义#的局部变量或方法'tenant'< ActionView::Base:>

rwqw0loc  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(126)

我目前正在尝试呈现一个带有错误消息的分部,如果表单提交无法创建tenant示例。我使用Rails文档中描述的locals将变量传递给分部。
控制器/租户_控制器.rb

def new
    @unit = Unit.find(params[:unit_id])
    @tenant = Tenant.new
  end

  def create
    @unit = Unit.find(params[:unit_id])
    @tenant = @unit.tenants.new(tenant_params)

    if @tenant.save
      redirect_to @tenant
    else
      Rails.logger.error('Unable to save Tenant')
      render 'new'
    end
  end

在这里,如果@tenant创建失败,我将重定向到new模板。这应该会向@tenant添加错误
租户/新.html.erb

<div class="py-6">
  <div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
    <h1 class="text-2xl font-semibold text-gray-900">
      Let's add the tenant's information
    </h1>
  </div>
  <div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
    <!-- Replace with your content -->
    <%= form_for [@unit, @tenant], html: { class: "space-y-8 divide-gray-200" } do |f| %>
     <%= render 'errors', locals: { tenant: f.object } %>
      <div class="space-y-8 divide-y divide-gray-200 sm:space-y-5">
        <div class="pt-8 space-y-6 sm:pt-10 sm:space-y-5">
          <div class="space-y-6 sm:space-y-5">
            <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
              <label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                <%= f.label :first_name, 'First name' %>
              </label>
              <div class="mt-1 sm:mt-0 sm:col-span-2">
                <%= f.text_field :first_name, value: "Albert", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
              </div>
            </div>
            <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
              <label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                <%= f.label :last_name, 'Last Name' %>
              </label>
              <div class="mt-1 sm:mt-0 sm:col-span-2">
                <%= f.text_field :last_name, value: "Terry", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
              </div>
            </div>
            <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
              <label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                <%= f.label :last_name, 'Last Name' %>
              </label>
              <div class="mt-1 sm:mt-0 sm:col-span-2">
                <%= f.text_field :last_name, value: "Terry", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
              </div>
            </div>
            <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
              <label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                <%= f.label :email, 'Email' %>
              </label>
              <div class="mt-1 sm:mt-0 sm:col-span-2">
                <%= f.text_field :email, value: "example@gmail.com", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
              </div>
            </div>
            <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
              <label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                <%= f.label :phone_number, 'Phone' %>
              </label>
              <div class="mt-1 sm:mt-0 sm:col-span-2">
                <%= f.text_field :phone_number, value: "(000)000-0000", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
              </div>
            </div>
            <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
              <label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                <%= f.label :lease_start_date, 'Lease start date' %>
              </label>
              <div class="mt-1 sm:mt-0 sm:col-span-2">
                <%= f.text_field :lease_start_date, value: "", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
              </div>
            </div>
            <div class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
              <label class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                <%= f.label :lease_end_date, 'Lease end date' %>
              </label>
              <div class="mt-1 sm:mt-0 sm:col-span-2">
                <%= f.text_field :lease_end_date, value: "", class: "block max-w-lg w-full shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm border-gray-300 rounded-md" %>
              </div>
            </div>
            <div class="sm:border-t sm:border-gray-200 sm:pt-5">
              <div>
                <%= f.label :notes, 'Add your notes & documents', class: "block text-sm font-medium text-gray-700" %>
                <div class="mt-1">
                  <%= f.rich_text_area :notes, class: "shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-full sm:text-sm border-gray-300 rounded-md" %>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="pt-5">
        <div class="flex justify-end">
          <%= link_to 'Cancel', unit_path(@unit), class: "bg-white py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
          <%= f.submit "Save", class: "ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
        </div>
      </div>
    <% end %>
    <!-- /End replace -->
  </div>

此处,im通过locals传递tenant变量,但它似乎没有将变量传递给<%= render 'errors', locals: { tenant: f.object } %>行中的部分错误
当呈现错误部分时,将引发错误。tenants/_errors.html.erb

<% if tenant.errors.any? %>
<div class="rounded-md bg-red-50 p-4">
  <div class="flex">
    <div class="flex-shrink-0">
      <!-- Heroicon name: mini/x-circle -->
      <svg class="h-5 w-5 text-red-400" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
        <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z" clip-rule="evenodd" />
      </svg>
    </div>
    <div class="ml-3">
      <h3 class="text-sm font-medium text-red-800">
        There were <%= tenant.errors.count %> errors with your submission
      </h3>
      <div class="mt-2 text-sm text-red-700">
        <ul role="list" class="list-disc space-y-1 pl-5">
          <% tenant.errors.full_messages.each do |error_message| %>
            <li><%= error_message %></li>
          <% end %>
        </ul>
      </div>
    </div>
  </div>
</div>
<% end %>

知道为什么吗
未定义#〈ActionView::Base的局部变量或方法'tenant'
正在被提升?

rdrgkggo

rdrgkggo1#

您呼叫render的方式不正确。
你有两个选择。你可以这样称呼它:

<%= render partial: 'errors', locals: { tenant: f.object } %>

或类似于:

<%= render 'errors', { tenant: f.object } %>

相关问题