在我的 Rails 控制器中,我有一个呈现特定布局的动作:
def registrations_for_user
render layout: 'remote_modal', locals: { modal_title: I18n.t('organizations.enrolled_in_learning_item.title') }
end
此布局需要本地
modal_title
才能工作,这就是我将其传递到那里的原因。这是布局:
<div class="relative px-4 w-full max-w-6xl md:h-auto">
<!-- Modal content -->
<div class="relative bg-white rounded-lg shadow dark:bg-gray-700">
<!-- Modal header -->
<div class="w-full">
<div class="flex justify-between items-start p-5 rounded-t">
<div class="w-full-0">
<h3 class="text-lg font-semibold text-gray-900 dark:text-white">
<%= modal_title %>
</h3>
</div>
<i class="close-modal fas fa-times text-lg cursor-pointer"></i>
</div>
</div>
<!-- Modal body -->
<div class="px-6">
<div class="w-full">
<%= yield %>
</div>
</div>
</div>
但是我不断得到:
NameError - undefined local variable or method `modal_title' for #<ActionView::Base:0x000000000656f8>:
app/views/layouts/remote_modal.html.erb:9
是否无法将本地传递给 rails 中的布局?还有另一种方法吗?我读过建议使用
@variable
的答案,但这似乎不是一个干净的方法。
你需要在你的控制器中定义它 根据路线,您可以做类似的事情
def local
@x = current_user.model.title #define your logic
end
然后在你看来
<%= @x %>