access rails url helper from deface - ruby-on-rails

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

Related

I need some help in the rails routing

Hey guys this is my routes file
Rails.application.routes.draw do
get 'home/store'
get 'home/chat'
get 'home/index'
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
and this is my controller
class HomeController < ApplicationController
def index
end
def chat
end
def store
end
end
and i am linking my three pages index.html.erb, chat.html.erb, store.html.erb which are in the home folder
like this in application.html.erb
<ul class="nav navbar-nav">
<li><link_to "Home","index.html></li>
<li><link_to "Buy Games","store.html"%></li>
<li><link_to "Watch Videos","#"%></li>
<li><link_to "Ask The Experts","chat.html"%></li>
</ul>
now the problem is that my root which is localhost:3000 is working fine but when i open any other link like chat through application.html.erb then the address bar is something like this "localhost:3000/chat.html.erb" but my location of file is in view/home/chat.html.erb same thing happens when i open any other link
so what should i do to make the routing work also i have tried putting home\chat.html in the link tag in apllication.html.erb it works but when i go back it adds another \home in the address bar
also my background images which i put in the inline style are displaying but when i open any other webpage like chat.html.erb then the inline css background images are not uploading
You need to
config/routes.rb
Rails.application.routes.draw do
get '/home/store', to: "home#store", as: :store
get '/home/chat', to: "home#chat", as: :chat
get '/home/index', to: "home#index", as: :index
root 'home#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
your application.html.erb
<ul class="nav navbar-nav">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Buy Games",store_path %></li>
<li><%= link_to "Ask The Experts", chat_path %></li>
</ul>
First of all, be sure to look over stack overflow's markdown guide to help you with styling your post in order to make it readable. Also, welcome to Stack Overflow :)
Okay, so it looks like you don't quite have the right syntax for specifying your links using rails' link_to helper with erb. The correct syntax is:
<%= link_to "my link", your_route %>
But it looks like you typed:
<link_to "Home","index.html>
You missed a closing " and didn't properly open or close the erb tags. So the correct syntax would be:
<%= link_to "Home", "index.html" %>
Then finally for all your links:
<li><%= link_to "Home", "index.html" %></li>
<li><%= link_to "Buy Games", "store.html" %></li>
<li><%= link_to "Watch Videos","#" %></li>
<li><%= link_to "Ask The Experts","chat.html" %></li>
be sure to look at the rails guides regarding the link_to helper for more information.
Hope that helps and good luck!

Seeking a better way to define my Rails routes/paths

I have 2 main components to my application, Users and Properties. The URL should be structured like: hostname.com/users/:user_id/properties/:property_id. I believe I've made a configuration error somewhere, because Rails never recognizes "property_path" or any of its variants, and I've had to hard code them in to get the redirects to work.
routes.rb
resources :users do
resources :properties
end
users/show.html.erb - Notice I had to hard code the path, instead of simply linking to "i"
<% #user.properties.each do |i| %>
<li><%= link_to "#{i.address}", "/users/#{#user.id}/properties/#{i.id}" %></li>
<% end %>
How can I better define my routes so that I can link above to just "i", which would represent the "properties_path", and would auto redirect to that show page?
You don't have to hardcode it. You can do:
<% #user.properties.each do |property| %>
<li><%= link_to property.address, [#user, property] %></li>
<% end %>
Yes, it's that simple. For more information, you can go to Rails guides.

Rails use ruby in link_to

Instead of hard coding a link description, I would like to use some Ruby code.
This is the original:
<li><%= link_to "Open Projects List", workorders_index2_path %></li>
This didn't work for me:
<li><%= link_to "<%= current_tenant.name_workorder.capitalize.pluralize %>", workorders_index2_path %></li>
Thanks for the help!
You don't need to use quotes at all:
<li><%= link_to current_tenant.name_workorder.capitalize.pluralize, workorders_index2_path %></li>
You already ARE using ruby code. <%= %> everything inside of that is pure ruby. link_to is a ruby method, and "Open Projects List" is the first parameter to that method, it's a string. Anything you can do in ruby you can send here - don't send a string, send the variable:
<li><%= link_to current_tenant.name_workorder.to_s.capitalize.pluralize, workorders_index2_path %></li>
You can also use string interpolation like you would with regular ruby:
<li><%= link_to "Open Project #{current_tenant.name_workorder}", workorders_index2_path %></li>

Can Ruby on Rails Automatically generate links to articles?

so my question is after i generate a scaffold and i have the ability to post articles to my site it usually is a link like www.mysite.com/articles/1
my question is...is it possible to have your application automatically generate a link to your www.mysite.com/articles/1 page
because right now i have to manually go into the HTML and add the link
=link_to 'my article', /articles/1
i was just wondering if its possible to make the application automatically generate the link for you?
Yes, it's possible. I don't know based on your question to what articles you would like to link, but if you have this set up using resources in your routes, this should work:
<ul>
<% Article.all do |article| %>
<li><%= link_to article.title, article %></li>
<% end %>
</ul>
Or if you're using HAML (as it appears you are based on the format of your question, but you don't specify:
%ul
- Article.all do |article|
%li= link_to article.title, article
You could modify the query to limit results, paginate them, sort them, etc. as you desire.

Rails 3.1 routing confusion

I am trying to create a link to a record in my application:
<ul class="presentation-box">
<% #presentations.each do |presentation| %>
<li><%= link_to "Presentation", presentations_path(#presentation) %></li>
<li><%= presentation.author %></li>
<% end %>
</ul>
With the following line in the routes file:
resources :presentations
root :to => 'presentations#index'
For some reason, when I click the link it's taking me to the Presentation index view. I believe it should be taking me to the show view of the individual record?
Am I missing something obvious?
Your link_to is incorrect.
presentations_path will actually point you to the index, you want presentation_path(presentation) to point directly to the resource.
Also, you can just do <%= link_to 'Presentation', presentation %> and Rails will build the correct path for you
Change it to presentation_path(presentation)

Resources