Rails:部分应该知道实例变量吗? [英] Rails: Should partials be aware of instance variables?

查看:44
本文介绍了Rails:部分应该知道实例变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,Ryan Bates 的 nifty_scaffolding 就是这样做的

Ryan Bates' nifty_scaffolding, for example, does this

edit.html.erb

edit.html.erb

<%= render :partial => 'form' %>

new.html.erb

new.html.erb

<%= render :partial => 'form' %>

_form.html.erb

_form.html.erb

<%= form_for @some_object_defined_in_action %>

那种隐藏状态让我感觉不舒服,所以我通常喜欢这样做

That hidden state makes me feel uncomfortable, so I usually like to do this

edit.html.erb

edit.html.erb

<%= render :partial => 'form', :locals => { :object => @my_object } %>

_form.html.erb

_form.html.erb

<%= form_for object %>

那么哪个更好:a) 让部分访问实例变量b) 向部分传递它需要的所有变量?

最近我一直在选择 b),但我确实遇到了一点泡菜:

I've been opting for b) as of late, but I did run into a little pickle:

some_action.html.erb

some_action.html.erb

<% @dad.sons.each do |a_son| %>
<%= render :partial => 'partial', :locals => { :son => a_son } %>
<% end %>

_partial.html.erb

_partial.html.erb

The son's name is <%= son.name %>
The dad's name is <%= son.dad.name %>

son.dad 调用数据库来获取父亲!所以我要么必须访问@dad,这将回到a) 有部分访问实例变量 或者我必须在本地传递@dad,将render :partial 更改为<%= render :partial => 'partial', :locals => { :dad => @dad, :son => a_son } %>,出于某种原因将一堆变量传递给我的部分让我感到不舒服.也许其他人也有这种感觉.

son.dad makes a database call to fetch the dad! So I would either have to access @dad, which would be going back to a) having partials access instance variables or I would have to pass @dad in locals, changing render :partial to <%= render :partial => 'partial', :locals => { :dad => @dad, :son => a_son } %>, and for some reason passing a bunch of vars to my partial makes me feel uncomfortable. Maybe others feel this way as well.

希望这是有道理的.正在寻找对整件事的一些见解...谢谢!

Hopefully that made some sense. Looking for some insight into this whole thing... Thanks!

推荐答案

在最新版本的 Rails 中,渲染局部变量并将局部变量传递给它们要容易得多.而不是这个.

In recent versions of Rails it is quite a bit easier to render partials and pass locals to them. Instead of this.

<%= render :partial => 'form', :locals => { :item => @item } %>

你可以这样做.

<%= render 'form', :item => @item %>

我不会在 Nifty Scaffold 生成器中这样做以保持向后兼容性,但我会在未来的版本中更改这一点.

I don't do this in the Nifty Scaffold generator to keep backwards compatibility, but I'll change this in a future release.

至于是否可以在partials中使用实例变量.我认为是这样.在所有实用性中,缺点是什么?当然,如果您不一致,事情可能会失控,但我喜欢应用这些准则.

As for whether it's acceptable to use instance variables in partials. I think it is. In all practicality, what is the downside? Certainly things can get out of hand if you aren't consistent, but I like to apply these guidelines.

  1. 永远不要创建一个实例变量只是为了在部分之间共享它.通常这意味着您将只共享控制器资源对象.

  1. Never create an instance variable just to share it between partials. Usually this means you will only be sharing the controller resource object.

如果部分与资源同名,则将其作为本地传递,并使用 <%= render @item %>.

If the partial is the same name as the resource, pass it as a local with <%= render @item %>.

如果部分将在多个控制器之间共享,则仅使用本地.

If the partial will be shared across multiple controllers then only use locals.

无论如何,这对我来说很管用.

This is what works well for me anyway.

额外提示:如果您发现自己将很多局部变量传递到局部变量中,并且希望其中一些是可选的,请创建一个渲染局部变量的辅助方法.然后总是通过 helper 方法,这样你就可以使用可选的 args 来创建一个干净的界面来呈现部分.

Bonus tip: if you find yourself passing in a lot of locals into a partial and you want some of them to be optional, create a helper method which renders the partial. Then always go through the helper method so you can make a clean interface with optional args for rendering the partial.

这篇关于Rails:部分应该知道实例变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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