Mustache and Haml - ruby-on-rails

I have got this haml/mustache template:
{{#data}}
ok
{{#items}}
{{#item}}
%b ID: {{id}}
{{/item}}
{{/items}}
{{/data}}
And I have got Illegal nesting: nesting within plain text is illegal Error.
I render it in Sinatra
Mustache.render(haml(:index), hash)

I'm not sure about rendering with Sinatra, but with this command:
cat example.yml foo.haml.mustache | mustache | haml -e
this data file example.yml
---
data:
- items:
- item:
- id: 1
- id: 2
- id: 3
---
and template (foo.haml.mustache ):
{{#data}}
#ok
{{#items}}
{{#item}}
%b ID: {{id}}
{{/item}}
{{/items}}
{{/data}}
I get following result:
<div id='ok'>
<b>ID: 1</b>
<b>ID: 2</b>
<b>ID: 3</b>
</div>
Pls pay attention to indentation level in *.mustache file. Hope this help you.

Related

Rails 6 internationalization - what has changed?

I started a fresh new Rails 6 project and got stuck to figure out why what obviously just worked (it's not the first app I start), fails...
So I created a dummy simply rails app with no additional gems and a home#index page:
<h1>Home#index</h1>
<p>
<%= t('hello.world') %>
</p>
Then I added a translation for the above key to config/locales/en.yml:
en:
hello: "Hello !"
world: "Hello World!"
and respected 1 tab indentation.
When navigating to localhost:3000/home/index I got the weird error:
/Users/serguei/.rvm/gems/ruby-2.7.0/gems/i18n-1.8.2/lib/i18n.rb:195: warning: The called method `translate' is defined here
Rendered home/index.html.erb within layouts/application (Duration: 6.2ms | Allocations: 3150)
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.0ms | Allocations: 5045)
ActionView::Template::Error (can not load translations from /Users/serguei/projects/rails/draft-app/config/locales/en.yml: #<Psych::SyntaxError: (<unknown>): did not find expected key while parsing a block mapping at line 2 column 3>):
1: <h1>Home#index</h1>
2: <p>
3: <%= t('hello.world') %>
4: </p>
app/views/home/index.html.erb:3
When changed the called translation to just hello:
<h1>Home#index</h1>
<p>
<%= t('hello') %>
</p>
and removing the last line from the en.yml file:
en:
hello: "Hello !"
it works.
Why so? What has changed since Rails 5 there? Can't we use nested translations anymore in locales files? Rails guides have nothing special about that. Or am I missing something?
Adding rails-i18n gem to the Gemfile didn't solve the problem.
Rails version: 6.0.2.1
Ruby version: ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]
If you want to nest it, then you can't assign a string value to the parent, do this instead
en:
hello:
world: "Hello World!"
Then, in erb, this will work
<%= t('hello.world') %>
Give it a try.

Improper indentation in converting ruby hash to yaml

I am trying to convert ruby hash object to YAML format using YAML.dump(obj) but I am getting improper indentation even after using dump options.
I have below executable ruby script :
#!/usr/bin/ruby
require "yaml"
require "erb"
context_path = ARGV[0]
context = YAML.load_file(context_path)['context']
def get_yaml(obj)
YAML.dump( obj['imports']['external_repositories']['credentials'] ).sub(/.*?\n/,'')
end
The value of - obj['imports']['external_repositories']['credentials'] is
{"iacbox"=>{"basic"=>{"name"=>"", "password"=>""}}, "nexus"=>{"basic"=>{"name"=>"cpreader", "password"=>"swordfish"}}}
Note : I used the sub method to remove "---" at the start of the output
The ERB template calls the above get_yaml method as :
credentials:
<%= get_yaml( context ) %>
The output that is coming is :
credentials:
iacbox:
basic:
name: ''
password: ''
nexus:
basic:
name: cpreader
password: swordfish
while I am expecting the output as :
credentials:
iacbox:
basic:
name: ''
password: ''
nexus:
basic:
name: cpreader
password: swordfish
How can I get the expected output from a dump?
I think the easiest thing for you to do here is just put the credentials key also in the Hash, i.e. change your template snippet so that it is one line:
<%= get_yaml( context ) %>
And change your get_yaml method to be:
def get_yaml(obj)
YAML.dump({'credentials' => obj['imports']['external_repositories']['credentials']})
.sub(/.*?\n/,'')
end
If that doesn't work for you, for example, if you have additional keys underneath the credentials key that you haven't mentioned, you could also do something like this:
def get_yaml(obj)
YAML.dump(obj['imports']['external_repositories']['credentials'])
.sub(/^---\n/,'')
.gsub(/\n/m,"\n ")
end
Where gsub(/\n/m,"\n ") replaces all newlines with a newline plus two spaces.

Rails 4: I18n::InvalidLocaleData

I installed the Kaminari gem and followed the instructions. I customized my theme to the foundations but when trying to further customize the pagination style using the en.yml file, I run into this error:
I18n::InvalidLocaleData in Campaigns#index
can not load translations from /myapp/config/locales/en.yml: #<Psych::SyntaxError: (/myapp/config/locales/en.yml): did not find expected key while parsing a block mapping at line 23 column 3>
Extracted source (around line #2):
1 <li>
2 <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, :remote => remote %>
3 </li>
I simply copied and pasted what was in the docs..
en:
hello: "Hello world"
pagination:
first: "« First"
last: "Last »"
previous: "‹ Prev"
next: "Next ›"
truncate: "…"
If I delete everything other than pagination: line, the error still remains. I'm not super familiar with the locale file in Rails as well as YAML files so any help is highly appreciated.
According to the results from YAML Lint, it appears that the hello: "Hello world" line is invalid.
Also, it looks like the views.pagination.first line in your code is looking for a path like: views -> pagination -> first in your YAML file (and because this pattern can't be found, there's another error).
Therefore, it might be better to use this kind of YAML example:
en:
views:
pagination:
first: "« First"
last: "Last »"
previous: "‹ Prev"
next: "Next ›"
truncate: "…"

allow html tags with mongoid + ruby on rails 3.2.8

I have this object y my database:
=> #<Page _id: 50683d421d41c8363d000060, _type: nil, created_at: 2012-09-30 12:38:26 UTC, title: "Terms", description: "Terms of use... <b>description</b>", published: true, slug: "terms">
On description field I have added with https://github.com/Nerian/bootstrap-wysihtml5-rails a b tag for bold.
But When I render my page I can see:
Terms of use... <b>description</b>
How can I render html tags on my mongodb database data?
Preview
http://www.image-share.com/ijpg-1766-71.html
Rails escapes html by default for security's sake.
When you trust content, tell Rails:
<%= raw your_variable %>
Or:
<%= your_variable.html_safe %>

Collection and Partials in Rails 3

This my first time asking a question so please go easy one me :-p
I am following the examples on http://guides.rubyonrails.org/layouts_and_rendering.html#using-partials (Section 3.4.5 Rendering Collections) for rendering collections using partials. The code looks simple, but clearly I am missing something.
models/expert.rb contains the line:
attr_accessible :name
experts_controller.rb contains the following line in the index method:
#experts = Expert.all
views/experts/index.html.erb contains the following line:
<%= render :partial => "expert", :collection => #experts %>
views/experts/_expert.html.erb contains:
<%= expert.name %>
Upon viewing the index page in my browser I get the following error:
NoMethodError in Experts#index
undefined method `name' for nil:NilClass
I have been working on this for an hour and am completely stumped :-/ What little thing am I missing?
---Clarification---
Running '<%= debug #experts %>' within index.html.erb produces the following output:
- !ruby/object:Expert
attributes:
id: 1
name: Bob Smith
slug: bob-smith
created_at: '2012-03-11 18:37:11.791118'
updated_at: '2012-03-11 18:55:58.179629'
changed_attributes: {}
previously_changed: {}
attributes_cache: {}
marked_for_destruction: false
destroyed: false
readonly: false
new_record: false
- !ruby/object:Expert
attributes:
id: 2
name: Steve Kamp
slug: steve-kamp
created_at: !!null
updated_at: !!null
changed_attributes: {}
previously_changed: {}
attributes_cache: {}
marked_for_destruction: false
destroyed: false
readonly: false
new_record: false
The exception almost certainly means that there are no experts, so #experts is an empty array. Have you created any expert records yet?
Also, just so you'll know, to render a collection of objects the way you're doing there's a nice shortcut:
render #experts

Resources