使用“@user”传递使用“用户”在“html.erb”中文件:如何以及为什么?

发布于 2024-10-17 14:56:05 字数 648 浏览 2 评论 0原文

我正在使用 Ruby on Rails 3,有人建议我@user 是一个全局变量,您无法“本地化”它。我建议以相同的方式从控制器传递局部变量。)使用像 user 这样的变量code> 而不是视图文件中的 @user到底为什么要这么做?

因此,我正在考虑从使用 @user 改为使用 user。也就是说,(在 html.erb 文件中)从 using

@user.name

到 using

user.name

此时,例如,在我的控制器的 show 方法中,我有:

def show
  @user = Users.find(1)
  ...
end

我必须在控制器中更改什么这在视图中有效吗?

I am using Ruby on Rails 3 and I was advised (@user is a global variable, you can't 'localize' it. I would suggest passing local variables from your controller in the same way.) to use variables like user instead of @user in view files. Why to do that, exactly?.

So, I am considering pass from using @user to using user. That is, (in html.erb file) from using

@user.name

to using

user.name

At this time, for example, in the show method of my controller I have:

def show
  @user = Users.find(1)
  ...
end

What I have to change in the controller to do that works in views?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丿*梦醉红颜 2024-10-24 14:56:05

只有当从多个控制器在视图中调用相同的部分时,您才需要担心这一点。

拥有使用@user的部分(可能在users_controller中设置),意味着当您从其他未设置@users的控制器(例如accounts_controller)的视图中调用该部分时,您将收到错误。如果您在部分中仅引用局部变量,则可以根据需要从任何控制器使用所描述的 :locals 哈希设置它们。

This is only something you need to worry about when the same partial is called in the views from more than one controller.

Having a partial that is using @user in it (likely set in a users_controller), means that the moment you call that partial in a view from some other controller (for example; accounts_controller) that does not set @users you will get an error. If you reference only local variables in your partial you can set them as needed from any controller with the :locals hash that was described.

长不大的小祸害 2024-10-24 14:56:05

这是没有意义的,只有instance_variables从控制器发送到视图。

Nikita Rybak 的回答并没有错,他只是将视图中包含的实例变量 (@current_user) 传递给具有不同名称的部分 (user):

 :locals => { :user => @current_user }

他总结得很好:

局部变量是局部的,所以不需要@来引用它们。

实际上,在使用部分时,您有两种选择:

  • 假设它可以访问实例变量(不建议这样做)

  • pass将实例变量传递给具有本地名称(Rails 方式)的部分

That's non sense, only instance_variables are sent from the controller to the view.

Nikita Rybak was not wrong in his answer, he just passed the instance variable contained in his view (@current_user) to a partial where it has a different name (user):

 :locals => { :user => @current_user }

he concluded very well:

Local variables are local, so you don't need @ to refer them.

Indeed you have two choices when working with a partial:

  • assume it has access to the instance variable (which is not advised)

  • pass the instance variable to the partial with a local name which is the Rails' way

清旖 2024-10-24 14:56:05

看看这个 Rails 最佳实践。渲染局部视图(以 _ 开头的视图文件)时,使用局部变量是更好的方法。这是因为您需要检查控制器的代码以了解实例变量。

Take a look at this Rails Best Practice. Using local variables is preferable way when rendering partials (those view files which start with a _). That's because you'll need review your controller's code to know about instance variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文