Ruby on rails应用程序助手在索引视图中不工作
我是RubyonRails的新手,我开始喜欢它了。我刚刚发现了在视图中使用helper的一个问题。我只是看不到我在助手中创建的指向索引视图的链接,但我可以从其他视图中看到链接。这有什么问题?请帮忙。谢谢。这是我的密码 应用程序\u helper.rbRuby on rails应用程序助手在索引视图中不工作,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,我是RubyonRails的新手,我开始喜欢它了。我刚刚发现了在视图中使用helper的一个问题。我只是看不到我在助手中创建的指向索引视图的链接,但我可以从其他视图中看到链接。这有什么问题?请帮忙。谢谢。这是我的密码 应用程序\u helper.rb module ApplicationHelper def main_menu link_to("Go to Main Menu", {controller: 'access', action: 'index'}) end end
module ApplicationHelper
def main_menu
link_to("Go to Main Menu", {controller: 'access', action: 'index'})
end
end
index.html.erb(助手不在此处工作)
管理员用户
输出:
edit.html.erb(助手在此处工作)
编辑用户表单
输出:
此助手方法将通过
控制器和操作查找路径(如果存在):
module ApplicationHelper
# @note `return`s below are for illustration but not required
# @see https://github.com/rails/journey
def link(txt, ctl, act)
begin
x = Rails.application.routes.routes.select {|x| x.defaults[:controller] == ctl and x.defaults[:action] == act}.first.path.spec.left.to_s
return link_to(txt.html_safe, x)
rescue => e
message = "ERROR route does not exist; controller: #{ctl}, action: #{act}; #{e.message}"
# log
Rails.logger.error(message)
# raise (not graceful)
raise message
# or return (graceful)
return nil
end
end
end
# in the view
link('dude awesome', 'controller_name', 'action_name')
#=> <a href="/controller/action_name">dude awesome</a>
方法上的参数不是必需的…请尝试此语法,看看您是否喜欢:
<%= main_menu %>
您还可以包括您的routes.rb吗?据我所知,您的代码示例对我来说很好。您的ApplicationHelper
本身没有什么问题。索引和编辑操作属于哪个控制器?
module ApplicationHelper
# @note `return`s below are for illustration but not required
# @see https://github.com/rails/journey
def link(txt, ctl, act)
begin
x = Rails.application.routes.routes.select {|x| x.defaults[:controller] == ctl and x.defaults[:action] == act}.first.path.spec.left.to_s
return link_to(txt.html_safe, x)
rescue => e
message = "ERROR route does not exist; controller: #{ctl}, action: #{act}; #{e.message}"
# log
Rails.logger.error(message)
# raise (not graceful)
raise message
# or return (graceful)
return nil
end
end
end
# in the view
link('dude awesome', 'controller_name', 'action_name')
#=> <a href="/controller/action_name">dude awesome</a>
def main_menu
link("Go to Main Menu", 'access', 'index')
end
<%= main_menu %>