使用动态变量局部渲染轨道 [英] Rendering rails partial with dynamic variables
问题描述
我正在尝试根据用户所在的分类单元来渲染部分内容.在我的application.html.erb布局中,有以下代码行:
I'm trying to render a partial based on the taxon the user is inside. In my application.html.erb layout I have the following line of code:
<%= render 'spree/shared/women_subnav' if @enable_women %>
在分类控制器中,在show方法内,我有:
In the taxons controller, inside the show method, I have:
@taxon_id = params[:id].split('/').first
在分类群中,我有:
<% if @taxon_id == params[:id].split('/').first %>
<%= "@enable_#{@taxon_id}" = true %>
<% end %>
运行此命令时,出现SyntaxError.但是在分类单元中#show如果我只输入:
When I run this I get a SyntaxError. But in taxons#show If I just enter:
<% if @taxon_id == params[:id].split('/').first %>
<%= "@enable_#{@taxon_id}" %>
<% end %>
没有'= true'的页面将呈现,输出'@enable_women'.所以我知道它获取了正确的变量,我只需要将该变量设置为true.我想念什么?
without the '= true' then the page renders, outputting '@enable_women'. So I know it's getting the correct variable, I just need that variable to be set to true. What am I missing?
非常感谢.
推荐答案
使用instance_variable_set
instance_variable_set "@enable_#{@taxon_id}", true
仅提醒您,最好在控制器内执行这些操作.
just a reminder that it's better to do these things inside a controller.
这篇关于使用动态变量局部渲染轨道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!