简体   繁体   中英

access rails url helper from deface

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!

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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