local_assigns 在 Rails 中是如何工作的? [英] How does local_assigns work in Rails?

查看:49
本文介绍了local_assigns 在 Rails 中是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌上搜索这个,但找不到正确的路径.我正在开发一个使用名为 local_assigns 的方法的 Rails 应用程序.这似乎是 Rails 或 gem 中的东西,并不特定于我的应用程序,但它用于在不同的上下文中呈现部分,例如:

I've been googling around about this and can't find the right path. I'm working on a Rails app that is using a method called local_assigns. This appears to be something in Rails or a gem, and not specific to my app, but it's used for rendering a partial in different contexts, such as this:

<% if local_assigns[:custom_name] %>
  <li><%= custom_name %></li>
<% else %>

或者还有这个:

<%= render "discussions/complementary/#{local_assigns[:action] || params[:action]}" %>

这是 Rails 方法吗?我在哪里可以找到更多关于此的文档?

Is this is Rails method? Where can I find more documentation about this?

推荐答案

local_assigns 是一个 Rails 视图辅助方法,你可以检查这个局部变量是否提供了局部变量.

local_assigns is a Rails view helper method that you can check whether this partial has been provided with local variables or not.

在这里,您使用一些值渲染部分,headlineperson 将可以通过预定义的值访问.

Here you render a partial with some values, the headline and person will become accessible with predefined value.

<%= render "shared/header", { :headline => "Welcome", :person => person } %>

shared/header 视图中:

Headline: <%= headline %>
First name: <%= person.first_name %>

以下是检查这些变量是否传入的方法:

Here is how you check these variables has passed in or not:

<% if local_assigns.has_key? :headline %>
  Headline: <%= headline %>
<% end %>

查看此文档以了解有关 将局部变量传递给子模板.

Check this document for more detail on the section Passing local variables to sub templates.

这篇关于local_assigns 在 Rails 中是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆