dynamic navigation links in main application and mounted rails engine - ruby-on-rails

I am working on a project to use rails engine whose views will be used inside of the main applications layout . My main application layout is made up of navigation links which is sort of like breadcrumbs which provides context to the user of how they reached that particular page.
So the question i had is : If i access the view of a mounted engine which renders the view inside of the main applications layout. how to make the navigation links which is part of the main application dynamic ?
Code Examples
module Engine
class ApplicationController < ::ApplicationController
end
end
Engines application controller uses main applications application controller which means that main applications layout will be used to render action views.
Main application application layout (using haml)
%html.no-js
%head
%title Main App
%body
.body-content
%main
= yield :navigation
.main-content
= yield
= site_footer
If you notice my application layout i have yield :navigation. This is rendered whenever there is a content_for method is used to define it. For example : lets say we are rendering an index action from one my main applications controller the haml would look like this
index.html.haml
- content_for :navigation do
%a.active{href: root_path}
%a{href: some_path}
end
%h1 Index page.
So the main question i have is if i render my engines controller action views the engine does not know how to build the navigation links for the main application. Is there any way that i can let main application define this and let the engine render the navigation links somehow.

Try using the main_app helper in your links.
%a.active{href: main_app.root_path}
Will allways reference you main apps root. Likewise
%a.active{href: my_engine.root_path}
Will always reference the engines root.
Or as said in the link below
If you wish to reference the application inside the engine in a similar way, use the main_app helper:
<%= link_to "Home", main_app.root_path %>
If you were to use this inside an engine, it would always go to the >application's root. If you were to leave off the main_app "routing proxy" >method call, it could potentially go to the engine's or application's >root, depending on where it was called from.
For fore information see:
http://guides.rubyonrails.org/engines.html#routes
/rpkn

Related

Rails engine extending views, not overriding

I am aware that I can override an applications view from within an engine by simply creating the same file within the engine and removing it from the application (eg: 'users/show.html.erb').
However, what I want is to be able to extend the applications view, not override.
Lets say I have a yield inside 'users/show.html.erb' of the main application:
yield :foo
What I want is for the engine to specify the same file 'users/show.html.erb' and to have a content_for block
content_for :foo {}
Thereby, injecting some template data from the engines view, into the applications view.
Obviously, the above won't work as once it has found the template file in the application, it won't look for one in the engine.
Is there a way to make this work?
There is no way to extend views that way in Rails. You can however, accomplish this by using partials. In your engine, write a partial view file users/_show.html.erb and then render it in your app's view:
# app/views/users/show
# will look for a partial called "_show.html.erb" in the engine's and app's view paths.
render partial: 'show'
It's just as easy as your suggestion.
This gem tries to implement partial extension of views, but it works similarly to what I just described: https://github.com/amatsuda/motorhead#partially-extending-views-in-the-main-app

How to render content from subfolder in a view?

I am using rails 4 from Suspenders and need to include content from a middleman-blog rack app from a subfolder in a view. In my app, I have the blog content in:
rails root > my_blog > source > index.html.erb.
I have created the view as:
rails root > app > views > welcomes > index.html.haml
The code in my view is:
%h1 Welcome
= render "my_blog/source/index.html.erb"
But when I access the page I get a Missing Partials error and the message says it only looked in the views folder.
How can I render content from a folder outside views?
Partial
The problem is Rails is trying to call a partial, which is not what you're trying to call. A partial should have its name preceded with an _underscore, indicating to Rails that it's a partial, hence why you're receiving your error
The reason this is important is because although you're just calling render, you will actually call the partial too
--
Convention
One of the issues you have is that you'll be going against convention in several ways:
MVC dictates the "view" will be loaded per request (so Rails will expect it to just be present whenever you use it)
The "partials" functionality of your system needs to add to the views you're showing the users. This means you have to be sure you
have a view already showing on screen
This means you need to be certain if you're meant to be using a partial or other element in this part of your app. From the looks of it, whilst you may be doing something right, you need to be sure you're able to load the partial correctly:
<%= render "your/partial/path/_partial_name.html.erb %>
--
View Path
Further to your view path issue, although I've never encountered this issue directly, there is a function called append_view_path, which allows you to add another "path" to look at for your app:
#app/controllers/welcome_controller.rb
Class WelcomeController < ApplicationController
append_view_path(File.join(RAILS_ROOT, "app/themes/#{#current_theme}"))
end
Try this is in your view file:
%h1 Welcome
= render :partial => "my_blog/source/index"
Note: You must have _index.html.erb partial file in the above specified path.
Also, try to change the name from _index.html.erb to _someothername.html.erb, because index.html.erb is usualy a view file for index action. You can avoid the unwanted confusions.
This may help you..!
Thanks!!
Added Lines (Edited):
Please change your file name from
rails root > my_blog > source > index.html.erb.
to
rails root > my_blog > source > _index.html.erb.
Have you tried the full path?
= render "#{Rails.root}/my_blog/source/index.html.haml"

How to use view and template based on language?

I am using high_voltage to create multiple landing pages.
Structure
app
views
pages
home.html.erb
about.html.erb
I would like to be able in simple way to use different view template according on session[:locale]
I thought about structure like this:
app
views
pages
en
home.html.erb
es
home.html.erb
But how then render correct template in PagesController?
I dont want to use Rails internationalization(I18n) gem for this.
I am saving I18n.locale in a Session using set_language_controller.
Source: http://xyzpub.com/en/ruby-on-rails/3.2/i18n_mehrsprachige_rails_applikation.html
Just name the files as:
app
views
pages
home.html.en.erb
home.html.es.erb
about.html.en.erb
about.html.es.erb

Access Main App Helpers when overridings a Rails Engine View/Layout

I have created a simple Rails Engine to provide some general functionality(photo gallery) to an application. I want to be able to override the standard _header partial so that the menu for the gallery matches that of my main application. In my header view I call a helper that is part of application_helpers (main app), but I keep getting "undefined method" errors. From what I can tell the main app application_helpers are not being included (obviously) when I override the engines application layout or its partials.
So my question is, how do I override an engine view in the main application, and get access to the main application helper methods? I would still need access to the engine helpers as well as not to screw up the engine functionality.
Do I need to override the controllers as well? seem like a lot just to get some helpers.
Thanks
Rails 3.1.3
check out this blog post: http://www.candland.net/2012/04/17/rails-routes-used-in-an-isolated-engine/
The author adds a method_missing definition to the application helper in order to access the parent application's helpers.
/config/initializers/blogit.rb
module Blogit
module ApplicationHelper
def method_missing method, *args, &block
puts "LOOKING FOR ROUTES #{method}"
if method.to_s.end_with?('_path') or method.to_s.end_with?('_url')
if main_app.respond_to?(method)
main_app.send(method, *args)
else
super
end
else
super
end
end
def respond_to?(method)
if method.to_s.end_with?('_path') or method.to_s.end_with?('_url')
if main_app.respond_to?(method)
true
else
super
end
else
super
end
end
end
end
Try including the main app helper methods. For instance:
class MyEngineClass
include ApplicationHelper
#...
end
You may possibly need to require the file first, though I would expect Rails to correctly find it in this case.
Once ApplicationHelper is included, you should be able to directly use those helpers in the controller.
It also looks like you can call ClassName.helper("application") for a lot of Rails classes -- not sure if that will work here.
try creating a helper in your application with the same name of the helper in your engine in order to override engine helper methods.
I found this discussion particularly insightful. There are also some interesting ideas in the Rails Engine API docs under Isolated engine helpers.
Engines are supposed to be independent from the main app, that is why you can't access its helpers from the Engine.
However, there are hack-ish ways for giving your engine access to the helpers of the main app. This is how I did it:
# In the main app
# initializers/share_helpers_path_with_engine.rb
PhotoGallery::Engine.class_eval do
paths["app/helpers"] << File.join(File.dirname(__FILE__), '../..', 'app/helpers')
end
You need of course to change PhotoGallery to the actual name of your engine class.
Feel free to take a look at the Engines documentation (section about the paths): http://edgeapi.rubyonrails.org/classes/Rails/Engine.html
Disclaimer: I've only used this solution in Rails 3.2 engines.
If, in your engine you have a standard header partial vendor/gems/my_gallery_engine/app/views/application/_header.html.erb.
Then, override it in your main app by creating a customized partial app/views/application/_header.html.erb.
The override works because Rails' view template search path (by default) starts with the main apps' app/views directory, and then searches through engines' app/views in load order.
All of your main app's Helpers will be available in the partial.

Rails 3.1 Mountable Engines : How to use/template it inside another application?

Let's say I created a mountable engine called 'Soho' that has a controller for 'Users'. I can go to /users/1 to see my user with ID 1.
Inside 'Soho', I have a application.html.erb for layout.
Now let's assume I want to "blend" my engine 'Soho' in an application called 'Soho_test', and I mount my engine at "/". So, in my host application 'Soho_test', I can also go at /users/1 to see my user with ID 1. This is working.
My question is : how can I do in my host application 'Soho_test' to apply the 'Soho_test' application.html.erb to the /users/1 (user profile page) instead of the one in the 'Soho' mountable engine?
Thanks!
I found how to achieve it, so I will post my answer on my own question, in case anyone else wonders. It is actually quite easy. I should have thought about this in the first place...
All you have to do is to create a folder in your /views/layouts/ with the name of your engine. So according to my question, it would be /views/layouts/soho/. In that folder, put the application.html.erb that you want to have.
You can do the same with partials and other views. Just create a folder /views/soho/.../ and put your files there. I havn't found a rake task to copy the engine views in my host application, so I wrote one.
After reading your question over a few times, I think all you are trying to do is override a layout for a given controller.
If that is the case just specify the layout to use within your controller see the section 2.2.13.1 Specifying Layouts on a per-Controller Basis within the Rails Guide for Layouts
Here's an example:
class UsersController < ApplicationController
layout "users"
#...
end

Resources