如何通过多次调用 render :partial 来跟踪信息
我正在使用 attribute_fu 为特定表呈现一个漂亮的行块。
<%= f.render_associated_form(@foo.bars, :new => 5) %>
我想让 bar
部分有一些状态的概念。 (因为这个概念特定于视图,所以我不想将其外部化到 Bar
模型本身并在控制器中计算它。)为了简单起见,假设它是 Bar
的索引@foo.bars
列表中的 >bar。
(我知道,如果是这种情况,我可以使用 :collection => @foo.bars 来启用 bar_counter...这在我的测试中似乎不起作用,但我已经看到了它的文档。)
我的问题-- 如何将变量传递到部分以便我可以保留和编辑状态? 天真地,我认为做类似的事情
<% @tmp = {:index => 1} %>
%= f.render_associated_form(@foo.bars, :new => 5, :locals => {:tmp => @tmp}) %>
#goes in the view
<%= tmp[:index] += 1 %>
会起作用。 tmp
被正确传递,但调用 [] 会抛出“呃哦,你刚刚在 nil 上调用了一个方法”。 令我惊讶的是,我可以执行 tmp.inspect、tmp.class 等来查看哈希,这些结果得到了我期望的结果。 但是 tmp[:index] 或 tmp[:anything_I_want] 会导致它爆炸。
将 tmp
制作为数组也有类似的结果。
有任何想法吗?
I am using attribute_fu to render a nice block of rows for a particular table.
<%= f.render_associated_form(@foo.bars, :new => 5) %>
I would like to have the bar
partial have some notion of a bit of state. (Because the notion is specific to the view, I do not want to externalize this to the Bar
model itself and calculate it in the controller.) For simplicity's sake, pretend it is the index of the bar
in the @foo.bars
list.
(I am aware that if this was the case I could use the :collection => @foo.bars to enable bar_counter... this doesn't appear to function in my tests but I have seen docs for it.)
My question -- how do I pass a variable into the partial such that I can keep and edit the state? Naively, I assumed that doing something like
<% @tmp = {:index => 1} %>
%= f.render_associated_form(@foo.bars, :new => 5, :locals => {:tmp => @tmp}) %>
#goes in the view
<%= tmp[:index] += 1 %>
would work. tmp
gets passed appropriately but calling [] throws "Uh oh, you just called a method on nil". Surprisingly to me, I can do tmp.inspect, tmp.class, etc to look at the Hash, and these have the results I would expect. But tmp[:index] or tmp[:anything_I_want] cause it to blow up.
Making tmp
an array had similar results.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终以彻底的 Rails 方式解决了这个问题——修补 :attribute_fu 以满足我的需求。 希望我能够很快向社区发布我的补丁。
I ended up solving this in a thoroughly Rails fashion -- patching :attribute_fu to meet my needs. Hopefully I'll be able to release my patches to the community fairly soon.
您上面描述的行为似乎一定是 attribute_fu 中的错误,因为本地没有正确传递,而它绝对应该正确传递。 我很想知道你做了什么来修补它。
The behavior you describe above seems like it must be a bug in attribute_fu, since the local isn't getting properly passed along, which it definitely should. I'd be interested to know what you did to patch it.