如何将当前控制器的名称输入到助手函数中?

时间:2022-03-07 23:45:33

I am still new to Rails and I have the following section_links that serve as the main navigation for my Rails app:

我还是Rails的新手,我有以下section_links作为我的Rails应用的主要导航:

<%= section_link(Organisation.model_name.human(:count => 2), 'organisations', organisations_path) %>
<%= section_link(Person.model_name.human(:count => 2), 'people', people_path) %>
<%= section_link(Project.model_name.human(:count => 2), 'projects', projects_path) %>           
<%= section_link(Invoice.model_name.human(:count => 2), 'invoices', invoices_path) %>       
<%= section_link(Payment.model_name.human(:count => 2), 'payments', payments_path) %>

I also wrote this very basic helper function:

我还写了一个非常基本的助手函数:

def section_link(title, section, path)
  options = {}  
  options[:class] = 'current' if section == controller_name
  link_to title, path, options
end

Is there any way to DRY up this helper function, so I can say something like this to make a link:

有没有什么方法可以让这个辅助函数失效呢?我可以这样说来建立一个链接:

<%= section_link("Organisations") %>

< % = section_link(“组织”)% >

I've been trying this for a couple of hours but I don't know how to pass the name of the current controller into my helper function.

我已经尝试了几个小时了,但是我不知道如何将当前控制器的名称传递给我的助手函数。

Thanks for any help with this...

谢谢你的帮忙…

4 个解决方案

#1


2  

Maybe something like

也许就像

def section_link(model)
   title = model.singularize.capitalize.constantize.model_name.human(:count => 2)
   section = model.downcase.pluralize
   path = {controller: model.pluralize.downcase, action: :index }
   options = {}  
   options[:class] = 'current' if section == controller_name
   link_to title, path, options
end

#2


2  

You can try request.params[:controller] to get controller name in helper. I'm not sure if it work, but I'm sure you can get controller name from params.

你可以试着请求。params[:controller]在helper中获取控制器名。我不确定它是否有效,但我确信您可以从params中获取控制器名。

#3


2  

Try: params[:controller] or controller.controller_name

试题:params[:控制器]或controller.controller_name

<%= section_link(params[:controller], Organisation.model_name.human(:count => 2), 'organisations', organisations_path) %>

def section_link(current_controller, title, section, path)
 options = {}  
 options[:class] = 'current' if section == controller_name
 link_to title, path, options
end

#4


1  

def current_link_to label, path, options = nil
    options ||= {} 
    options[:class] =  [options[:class], (current_page?(path) ? "active" : nil)].compact.join(" ")
    link_to label, path, options
end

#1


2  

Maybe something like

也许就像

def section_link(model)
   title = model.singularize.capitalize.constantize.model_name.human(:count => 2)
   section = model.downcase.pluralize
   path = {controller: model.pluralize.downcase, action: :index }
   options = {}  
   options[:class] = 'current' if section == controller_name
   link_to title, path, options
end

#2


2  

You can try request.params[:controller] to get controller name in helper. I'm not sure if it work, but I'm sure you can get controller name from params.

你可以试着请求。params[:controller]在helper中获取控制器名。我不确定它是否有效,但我确信您可以从params中获取控制器名。

#3


2  

Try: params[:controller] or controller.controller_name

试题:params[:控制器]或controller.controller_name

<%= section_link(params[:controller], Organisation.model_name.human(:count => 2), 'organisations', organisations_path) %>

def section_link(current_controller, title, section, path)
 options = {}  
 options[:class] = 'current' if section == controller_name
 link_to title, path, options
end

#4


1  

def current_link_to label, path, options = nil
    options ||= {} 
    options[:class] =  [options[:class], (current_page?(path) ? "active" : nil)].compact.join(" ")
    link_to label, path, options
end