access rails url helper from deface

女生的网名这么多〃 提交于 2019-12-11 04:56:21

问题


I am including Spree to an existing site. I am changing the spree header with Deface to render my site's header instead. Therefore I use the following Deface DSL code

<!-- replace_contents "header[data-hook]" -->
<%= render :partial => "layouts/my_site_header.html.erb" %>

And inside _my_site_header.html.erb I have something like this

<ul>
    <li><%= link_to "Home", home_path %></li>
    <li><%= link_to "Game", game_path %></li>
    <li><%= link_to "Community", community_path %></li>
</ul>

Which gives me the following error

undefined local variable or method `home_path' for #<#<Class:0x8a73c20>:0x8af0e58>

I understood that the code get executed by Deface in the Spree scope, thus my site's url helpers are undefined. I could solve this using the full method name like Rails.application.routes.url_helpers.home_path

However, I don't really feel like adding this for all of my links. Isn't there a way to tell Spree to include the url helpers of my site? Please help!


回答1:


There is a shorter version which you can use from Rails::Engine called main_app.

<ul>
    <li><%= link_to "Home", main_app.home_path %></li>
    <li><%= link_to "Game", main_app.game_path %></li>
    <li><%= link_to "Community", main_app.community_path %></li>
</ul>

I would highly recommend using this to avoid conflicts between your application and Spree (such as your app home vs. Spree's home).



来源:https://stackoverflow.com/questions/18294079/access-rails-url-helper-from-deface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!