我正在尝试构建一个名为的助手
def current_project
@current_project = Project.find(params[:project_id])
end
我想要这个,以便在我使用的导航插件中我可以使用 current_project 来填充导航。
我遇到的问题是,当 params[:project_id] 在导航配置 (navigation.rb) 中可用时,它在 application_helper.rb 中不起作用。我收到以下错误:
“find_with_ids':找不到没有 ID 的项目”
这是导航插件,如果你好奇的话:http://github.com/andi/simple-navigation/
为什么?
谢谢
最佳答案
如果将 project_id 传递给 helper 不是一个选项,您可以将该方法放入 Controller 并将其标记为 helper_method
.例如:
class ApplicationController < ActionController::Base
helper_method :current_project
private
def current_project
@current_project ||= Project.find(params[:project_id])
end
end
然后在您的 View 中,您可以使用以下命令调用它:
<% current_project %>
如你所料。
关于ruby-on-rails - 访问 URL 参数 Project.find(params[ :project_id]) in the application_helper. rb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3851651/