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
Related
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
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"
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
I have two RoR3 applications (APP1 and APP2)
www.subdomain1.example.com
www.subdomain2.example.com
and I want to show on APP1 some views from APP2.
I tried to do that using a 'Net::HTTP' request (code in APP1)
Net::HTTP.get( URI.parse("http://www.subdomain2.example.com/users/new") )
but the response is not evaluated as HTTP code. Among other things I do not know if there are other techniques to do what I want in more easy way.
So, is it possible to render partials from APP1 to APP2 using the common and easy approach of rendering partials in the same RoR application?
Example:
render :partial => "/users/new"
If so, how can I do that?
Here, try this:
module ApplicationHelper
require 'open-uri'
def render_url(url)
open url do |f|
f.read.html_safe # remove the 'html_safe' if you're on Rails 2.x
end
end
end
In your view:
<%= render_url 'http://ilikestuffblog.com/' %>
It will work. Just one problem, though: if the site contains relative links to images, other pages, or anything else, those links will not be shown correctly. Try this to see a bunch of blank images:
<%= render_url 'http://www.ducklet.com/' %>
Also, BE WARNED that if you don't own the URL you're including, you will be subject to cross-site scripting weirdness.
If the two applications share a filesystem or have access to a shared filesystem, then you can reference a partial directly by file path. From the Rails guide on rendering:
2.2.4 Rendering an Arbitrary File
The render method can also use a view
that’s entirely outside of your
application (perhaps you’re sharing
views between two Rails applications):
render
"/u/apps/warehouse_app/current/app/views/products/show"
Rails determines that this is a file
render because of the leading slash
character. To be explicit, you can use
the :file option (which was required
on Rails 2.2 and earlier):
render :file =>
"/u/apps/warehouse_app/current/app/views/products/show"
The :file option takes an absolute
file-system path. Of course, you need
to have rights to the view that you’re
using to render the content.
It might be more prudent to create a gem that has any shared code (ie. partials) in it so both apps can use it.
Is there a simple way to define a master template for my whole rails application? If not, what's the best way to reuse my templates so that I'm not copy and pasting the same template into a bunch of layout files?
You can name it application.html.erb and Rails will use it for the whole app.
More info at rails guides.
Create an application.html.erb file in the layout folder of the views. It will be called if the controller has no template, so you might need to remove them.
You can also define a template for a specific controller going
class FaqentriesController < ApplicationController
layout "admin"
[..]
/app/views/layouts/whatever.rhtml (or whichever extension your prefer to work with):
<html>
...
<%= yield %>
...
</html>
/app/controllers/ApplicationController.rb:
layout "whatever"
(Edit: I can't remember off the top of my head whether calling the layout application.rhtml (or whatever) automatically makes it the default layout for any controller lacking specification or whether this bit of magic is incorporated into the default ApplicationController when you generate scaffolding, using the above syntax.)