轨道:#<ActionView::Base:>的未定义局部变量或方法"租户"



我目前正试图呈现部分错误消息,如果当一个表单提交未能创建一个tenant实例。我使用Rails文档中描述的locals将变量传递给局部。

控制器/tenants_controller.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

租户/new.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>

这里我通过locals传递tenant变量,但是它似乎没有将变量传递给<%= render 'errors', locals: { tenant: f.object } %>

行的部分错误。当errors部分被呈现时,会引发一个错误。租户/_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 %>

知道为什么

未定义的本地变量或方法' tenant'用于#

正在升高?

呼叫render错误。

你有两个选择。你可以这样调用它:

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

或者像这样:

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

相关内容

最新更新