简体   繁体   English

在Rails中创建动态助手方法

[英]creating dynamic helper methods in rails

I am trying to create a bunch of dynamic helper methods like these: 我正在尝试创建一堆像这样的动态助手方法:

show_admin_sidebar
show_posts_sidebar
show_users_sidebar

So far I have this in my helper.rb file: 到目前为止,我的helper.rb文件中包含以下内容:

#spits out a partial 
def show_sidebar(name, show_sidebar = true)
  @content_for_sidebar = render :partial => "partials/#{name}"                       
  @show_sidebar = show_sidebar
end

def show_sidebar?
  @show_sidebar
end

In my application layout file I have this: (NB - I'm using HAML): 在我的应用程序布局文件中,我有以下内容:(注意-我正在使用HAML):

- if show_sidebar?
  = yield(:sidebar)

This allows me to say the following in my views: 这使我可以发表以下看法:

- show_sidebar(:foo)
- show_sidebar(:bar)

And this renders the desired partial. 这将呈现所需的局部。

The problem with this is that I can only add one sidebar per page. 问题是我每页只能添加一个侧边栏。 So, I figure I need to have dynamic methods like: show_admin_sidebar, show_foo_sidebar. 因此,我认为我需要动态方法,例如:show_admin_sidebar,show_foo_sidebar。

So I have tried to do this: 所以我试图做到这一点:

def show_#{name}_sidebar(show_sidebar = true)
@name = name  
@content_for_#{@name}_sidebar = render :partial => "partials/#{@name}"                       
  @show_sidebar = show_sidebar
end

and then in my layout: 然后在我的布局中:

- if show_sidebar?
  = yield("{@name}_sidebar")

But rails does not like this at all. 但是Rails根本不喜欢这样。

I have tried almost everything I can think of in my helper file and nothing works. 我已经尝试了我可以在帮助文件中想到的几乎所有内容,但没有任何效果。

The reason I am using helper methods for this is because I want my content div to be 100% page width unless there is a sidebar present in which case the main content goes into a smaller div and the sidebar content goes into it's own.. 我之所以使用辅助方法的原因是,我希望我的内容div的页面宽度为100%,除非存在侧边栏,在这种情况下,主要内容将进入较小的div,侧边栏内容将进入其自身。

If I can't get this working, then I can easily fix the problem by just adding the partials manually but I'd like to get my head round this.... 如果无法正常工作,则可以通过手动添加局部零件轻松解决此问题,但是我想解决这个问题。

Anyone got any experience with this kind of thing? 任何人都对这种事情有经验吗?

The entire approach to this was bizarrely overcomplicated, didn't follow Rails conventions at all, nor make the slightest bit of sense, and shame on prior respondents for enabling this approach instead of helping him to simplify. 整个方法极其复杂,根本没有遵循Rails的约定,也没有丝毫意义,对于启用此方法而不是帮助他简化的以前的受访者感到羞耻。 My apologies for being 13 months late with the answer. 很抱歉迟到了13个月。

Your controller should be deciding if a sidebar is to be shown or not, and setting an instance variable @side_bar_name to either nil or a sidebar name string. 您的控制器应决定是否显示侧边栏,并将实例变量@side_bar_name设置为nil或侧边栏名称字符串。 Then somewhere in shared view code, probably views/layouts/application.html.erb, you would have something as simple as this: 然后,在共享视图代码中的某处(可能是views / layouts / application.html.erb),您将像下面这样简单:

<% if @side_bar_name %>
<%= render :partial => "partials/#{@side_bar_name}" %>
<% end %>

Or better yet: 或者更好:

<%= render(:partial => "partials/#{@side_bar_name}") if @side_bar_name %>

If you want to use a helper (which is not a bad idea for keeping your code DRY and readable) it would basically be the same code, just moved into the helper. 如果您要使用帮助程序(这对于保持代码的DRY和可读性来说不是一个坏主意),则基本上是相同的代码,只需移入该帮助程序即可。

<%= side_bar_helper %>

def side_bar_helper
  render(:partial => "partials/#{@side_bar_name}") if @side_bar_name
end

What the controller does is up to you. 控制器的工作由您决定。 It would probably do something like this: 它可能会执行以下操作:

if session[:show_side_bar]
  # maybe use cookies instead of session, or store user preference in a database
  @side_bar_name = session[:side_bar_name]
end

Here is a solution for you, however I wouldn't suggest too much metaprogramming: 这是为您提供的解决方案,但是我不建议过多的元编程:

#Add the following snippet to the proper helper module:
['admin','user','whatever'].each do |name|
  class_eval{
    "def show_#{name}_sidebar(show_sidebar = true)
       @name = #{name}  
       @content_for_#{@name}_sidebar = render :partial => 'partials/#{@name}'                       
       @show_sidebar = show_sidebar
     end"
  }
end

def show_#{name}_sidebar(show_sidebar = true) def show _#{name} _sidebar(show_sidebar = true)

That doesn't look like valid Ruby to me. 在我看来,这不是有效的Ruby。 Are you parsing and evaling this yourself or just throwing that right in the file and expecting it to work? 您是自己解析和逃避此问题,还是只是将其扔到文件中并期望它能正常工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM