I want to create a Ruby on Rails layout and it should be in Liquid format.
Basically what I'm trying to do is to let the users to create their own layouts and save them in the database.
I tried to use
<%= Liquid::Template.parse(<code from database>).render %> in my layout.erb file but there I can't use the 'yield' command (since this is a layout I should have to have a way of rendering pages.)
But when I use 'layout.liquid' with {{ content_for_layout }} is will work find BUT, cannot load details from the database (I mean the HTML code..)
I hope I made myself clear :D )
Take a look at this Ruby on Rails plug-in:
http://github.com/akitaonrails/dynamic_liquid_templates
Next we have to find a way to intercept the default Ruby on Rails behaviour for your controller.
class MyAwesomeController
layout :get_my_db_layout
....
protected
def get_my_db_layout
'as_if_by_a_miracle.liquid' # add your db finder here
end
end
Then, overwrite LocalFileSystem#read_template_file with your own class / method, to get the template from the database. LocalFileSystem#read_template_file is a Liquid class.
I hope, that this idea is helpful.
please read Tobis article on
https://github.com/shopify/liquid/wiki/getting-liquid-to-work-in-rails
or look at this screencast
http://railscasts.com/episodes/118-liquid
Related
When I try to render a partial from a helper, it fails with this (condensed) error message:
Missing partial /_cube_icon with [...]. Searched in:
Note that the list of searched directories is empty!
In contrast, when using render in a view, it knows where to look:
Searched in: * "/Users/Lars/GitHub/algdb/app/views"
In the helper code, I use ActionController::Base.helpers.render(). Should I use some other render function? How do I tell it where to look for partials? Could I have set up the project wrong somehow?
This is Rails 4.2.4 ยท Ruby 2.3.1
OK, I figured it out:
I was calling render from code in the helper directory, but not from a function in a standard SomethingHelper module.
When following that convention, things started working.
Partial files normally reside within the app/views directory and are not located in the helpers directory in Rails as you can see from the error message you are receiving. To solve your problem, I would move the _cube_icon file into the app/views directory and organize your code there. I recommend reading this section of the Rails documentation for views Layouts and Rendering
Additionally, it sounds like you may be new to rails so I would take a look at the conventions that rails offers. Rails, as you may or may not know, is an opinionated framework which means certain things need to go in certain locations in order for it to work. Here is another resource on just the view part of Rail's MVC framework Action View Overview. Hope this helps.
---Updated-----
If you really want to render a partial from a helper file, there are a few ways to do so but the best one to use is outlined below:
In app/helpers
module MyHelper
def custom_render
concat(render(:partial => 'cube_icon'))
end
end
Here are some links from stackoverflow to help you out.
Rendering a partial from helper #1
Rendering from a partial from herlper #2
Using concat rails
I'm trying to get custom scaffolding working from my engine.
I followed some tutorial on customizing Rails 3.2 scaffolding in a normal Rails App and put my customized templates in the engines /lib/templates/erb/scaffold directory but they don't get picked up by the app that includes the engine. Any suggestions?
Update:
I also tried to override the Rails ScaffoldGenerator's source_path and tried some other paths to put my template in, like:
lib/rails/generators/erb/scaffold/templates
zarazan's answer got me most of the way there, but there are a couple of things wrong with it. Here's what worked for me:
class Engine < Rails::Engine
config.generators do |g|
g.templates.unshift File::expand_path('../../templates', __FILE__)
end
end
Note that this goes in the generators section, not app_generators, and that the path is slightly different.
Also, I think the correct path to store your templates is lib/templates/erb/scaffold, optionally replacing erb with whatever language you are using (like haml or slim.) I know this works for slim. The file names are {_form,edit,index,new,show}.html.erb.
In the file that you declare your engine use this command:
class Engine < Rails::Engine
config.app_generators do |g|
g.templates.unshift File::expand_path('../templates', __FILE__)
end
end
It should shift the preference of what template folder Rails uses by default.
Now just put the template files in lib/templates/erb/scaffold/template_name.erb
Where template_name is one of the following: _form.html.erb, edit.html.erb, index.html.erb, new.html.erb, show.html.erb
Once you include the gem you should be able to use the rails generate scaffold command as normal.
Here is an example of an engine that overrides the default scaffolding in rails:
https://github.com/brocktoncg/gemboree
This is where the template directory is located:
https://github.com/brocktoncg/gemboree/tree/master/lib/templates/erb/scaffold
Are you talking about a controller template? Than you are using the wrong directory. Save your template at
lib/templates/rails/scaffold_controller/controller.rb
Have a look at http://xyzpub.com/en/ruby-on-rails/3.2/templates.html for an example.
My rails app is using RDiscount to generate HTML from user-supplied markdown text, and I noticed that the anchor tags don't have rel="nofollow". This is a big issue for me as my app is open to the public. Is there a way to enable nofollow links, or are there better solutions?
Thanks!
I think this is possible only with Kramdown, which is a ruby Markdown parser with extended syntax. You would do that then as shown in the link:
[link](test.html){:rel='nofollow'}
In the mean time, I am using this hack, by re-parsing the RDiscount output and add a rel="nofollow" to each anchor:
def markdown(input)
html = RDiscount.new(input).to_html
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css("a").each do |link|
link['rel'] = 'nofollow'
end
doc.to_html
end
Though I think this should really be handled by the markdown parser.
I needed to do something similar, add target="_new" to all links. Solved it using Kramdown and a custom Kramdown::Converter::Html class.
Define a Kramdown::Converter::Html subclass (kramdown/converter/my_html.rb in some autoload path)
class Kramdown::Converter::MyHtml < Kramdown::Converter::Html
def convert_a(el, indent)
el.attr['target'] = '_new'
super
end
end
I also have a view helper in app/helpers/application_helper.rb
def markdown(str)
Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe
end
Ideally it should be possible to just use Kramdown::Document.new(str).to_my_html.html_safe but I can't get it to work in rails development mode as Kramdown uses const_defined? to see if a converter is available and that does not trigger the autoloader. Please comment if you know how to fix this.
There is an open feature request on RDiscount to support modifying links in this fashion.
It is scheduled for one of the upcoming RDiscount 2.1.5.x releases.
I have an application with Rails 2.3.5. And Im trying to use AS latest version, I have used it previously but cant make it work here.
I have my ingredient_categories Controller, where i put
class Administration::IngredientCategoriesController < ApplicationController
layout "default"
active_scaffold :ingredient_categories
end
I have this set up on routes to be :active_scaffold=>true
I have a model also called ingredient_category, and in the views folder (inside administration/ingredient_categories, and /ingredient_categories) i have nothing as it is usual.
And Im getting over and over again:
Template is missing
Missing template ingredient_categories/list.html in view path themes/aqueouslight:app/views
I had an error before asking me for a list.erb, which I created and put
'ingredient_categories', :label => 'Categorias' %>
And now this error of the list.thml...
Cant make it work! dont know whhy really... whould be SO simple and its burning my head now..
Thanks!
Make sure you have the render_component plugin installed, as it was deprecated from rails 2.3.x and is used by active scaffold
This may not be the cause of your particular problem but you will have trouble with it if you combine AS+Rails2.3.x
I hope it helps
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.)