I was looking into How to use rails-i18n with HAML to find out how i18n works together with haml but ran into an issue I can't figure out.
This works:
en.yml
en:
sitename: "Happy Sunday"
new.haml
%h1= t("sitename")
When I change the yml to
en.yml
en:
home:
sitename: "Happy Sunday"
new.haml
%h1= t("home.sitename")
Then I get the following error:
ArgumentError in Devise/sessions#new
Showing
..../devise/sessions/new.html.haml where line #20 raised:
syntax error on line 4, col 6: ` home:'
Extracted source (around line #20):
17: = flash[:alert]
18: .row
19: .headline.pagination-centered
20: %h1= t("home.sitename")
21: %h2= t("slogan")
22: .row.headline.pagination-centered
23: %a{:href => "/tour"}
The message:
syntax error on line 4, col 6: ` home:'
suggests an error in your Yaml. Check en.yml, especially that you’re not using tabs and that your indentation is consistent.
Related
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.
I am currently working on an RSS feed that was working for a while but now has an encoding issue. After trying many of the solutions here unsuccessfully, I have deduced that I may be having issues with the feed parser.
The error starts with the index page:
ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8):
11:
12: <div class="entry_wrapper">
13: <div class="entry_box">
14: <% feed.entries.each do |entry| %>
15: <p class="entry_title"><%= sanitize link_to entry.title, entry.url %></p>
16:
17:
app/models/feed.rb:6:in `entries'
app/views/feeds/index.html.erb:14:in `block in _app_views_feeds_index_html_erb__2672739530113604393_70126099705280'
app/views/feeds/index.html.erb:5:in `_app_views_feeds_index_html_erb__2672739530113604393_70126099705280'
Here is larger section of the Feed#Index page:
<% #feeds.each do |feed| %> # Line 5 here
<p class="feed_url">
<%= link_to feed.url, feed %>
<%= link_to "Edit", edit_feed_path(feed), class: "blue" %>
<%= link_to "Delete", feed_path(feed), method: :delete, data: { confirm: 'Are you sure you want to delete this feed url?' }, class: "blue" %>
</p>
<div class="entry_wrapper">
<div class="entry_box">
<% feed.entries.each do |entry| %>
<p class="entry_title"><%= sanitize link_to entry.title, entry.url %></p>
I deleted the Feed URL's in the console so the page rendered fine without any urls to pull from. However, as soon as I added one, I got the same error as before.
I tried testing the encoding of the Feed entries in the console and got the following error:
2.1.1 :001 > g = Feed.last
Feed Load (0.1ms) SELECT "feeds".* FROM "feeds" ORDER BY "feeds"."id" DESC LIMIT 1
=> #<Feed id: 9, name: nil, created_at: "2016-01-18 05:01:54", updated_at: "2016-01-18 05:01:54", url: "http://feeds.feedburner.com/MattsTravelSite">
2.1.1 :002 > g.entries
Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/entities.rb:77:in `gsub'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/entities.rb:77:in `decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/entity_decoder.rb:14:in `decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/entity_decoder.rb:5:in `try_decode'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/sax_parser.rb:151:in `on_text'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:541:in `_rule_33'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:239:in `block in each_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:237:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:237:in `add_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:439:in `on_element_end'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:190:in `advance_native'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:190:in `block in advance'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:137:in `read_data'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/lexer.rb:189:in `advance'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:236:in `each_token'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/oga-2.0.0/lib/oga/xml/parser.rb:269:in `parse'
... 20 levels...
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `block in load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /Users/danieluribe/.rvm/gems/ruby-2.1.1/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/danieluribe/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/danieluribe/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
I double-checked to see if the website I am pulling from is in UTF-8 encoding at the W3C-Validator website:
This document was successfully checked as well-formed XML!
Result: Passed, 1 warning(s)
Address:
http://feeds.feedburner.com/MattsTravelSite
Encoding: utf-8
Doctype: XML
Root Element: feed
Root Namespace: http://www.w3.org/2005/Atom
I tried "forcing" the encoding in the model, but I think my coding skills are a little lacking in terms of creating methods. Here is the entries model I created originally for the Feed that is being used in the Feed#Index.
class Feed < ActiveRecord::Base
has_many :items
def entries(num = 3)
Feedjira::Feed.add_common_feed_entry_element("img")
feed = Feedjira::Feed.fetch_and_parse(url)
feed.entries.take(num)
# add_items(feed.entries) # Saving items to database
end
....
Since this model was working before, I'm not sure what happened. I did take a few months from the app so I don't know if anything changed with Feedjirra. Any help would be greatly appreciated.
I am getting a weird syntax error from rails that I don't understand.
GETTING THE FOLLOWING ERROR:
Showing /home/action/workspace/clinio/app/views/tasks/_task.html.erb where line #3 raised:
/home/action/workspace/clinio/app/views/tasks/_task.html.erb:3: syntax error, unexpected ';', expecting ':'
';#output_buffer.append=( image...
^
Extracted source (around line #3):
<% #uncompletedtasks = #task if #uncompletedtasks?%>
<li id="task_">
<div><%= image_tag "26-mini-gray-checkmark.png" %>
<%= #uncompletedtasks.task %>
</div>
</li>
Trace of template inclusion: app/views/tasks/_task.html.erb, app/views/layouts/application.html.erb
Rails.root: /home/action/workspace/clinio
Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:35:in _app_views_layouts_application_html_erb__122972711486791642_46610700'
app/controllers/users_controller.rb:16:inindex'
You don't need that question mark at the end.
<% #uncompletedtasks = #task if #uncompletedtasks %>
(the purpose of this code still eludes me, though. Why would you want overwrite #uncompletedtasks only if it has value?)
It was working few days ago, don't know what had went wrong...
**undefined method `name' for nil:NilClass**
Extracted source (around line #26):
23: %td= number_with_precision(employee.compensation_leave_balance, precision:1)
24: #calendar.tab-pane.fade
25: = calendar(:year => 2012, :month => 6, :first_day_of_week => 1, summary: "Leave Calendar", calendar_title: "June", month_header: true) do |date|
26: - render_leave_calendar_cell(date)
27: #trash.tab-pane.fade
28: = render 'table', leaves: #leaves.where(deleted: true)
app/helpers/leaves_helper.rb:11:in `block in events_for'
app/helpers/leaves_helper.rb:10:in `events_for'
app/helpers/leaves_helper.rb:4:in `render_leave_calendar_cell'
app/views/human_resources/leaves/index.html.haml:26:in `block in _app_views_human_resources_leaves_index_html_haml__145883348_88978910'
app/helpers/calendar_helper.rb:146:in `call'
app/helpers/calendar_helper.rb:146:in `block in calendar'
app/helpers/calendar_helper.rb:145:in `upto'
app/helpers/calendar_helper.rb:145:in `calendar'
app/views/human_resources/leaves/index.html.haml:25:in `_app_views_human_resources_leaves_index_html_haml__145883348_88978910'
really don't know what went wrong
a/h/leaves_helper.rb
1 module LeavesHelper
2 def render_leave_calendar_cell(date)
3 html = content_tag(:span, date.day, class: 'dayDisplay')
4 html += content_tag(:div, events_for(date))
5 raw(html)
6 end
7
8 def events_for(date)
9 html = ""
10 current_company.leaves.where("start_date <= '#{date}' and return_date > '#{date}'").where(deleted: false).each do |leave|
11 html += content_tag(:div, leave.applicant.name, class: 'leaveName')
12 end
13 raw html
14 end
could it be the date nil? how to fix this ><
much appreciate
Billy
as abhas already stated, leave.applicant is nil for at least one of the leaves.
go to your database and find out which it is. then figure out what to do with your leaves. delete them too, re-add the missing applicant or what ever data migration might be sensible.
a quick fix would be to skip if an applicant is missing:
html += content_tag(:div, leave.applicant.name, class: 'leaveName') if leave.applicant.present?
i would also have a look if you properly configured the delete cascades in your application. this often causes such problems. if you want to enforce safety in this regard, you should add database constraints, that ensure that no referenced entity gets deleted.
Im trying to find all records that have similar tags to the currently viewed record.
My controller has:
def show
#tattoo =Tattoo.find(params[:id])
tags = #tattoo.style_list.join(", ")
#tattoos = Tattoo.tagged_with(tags, :any => true).limit(6)
end
(bonus points if anyone can tell me how to randomize the order of records in the arrary)
My view just loops through the array.
Anyway, it works almost all the time but I noticed it breaks occasionally and while troubleshooting I found that it breaks when I use tagged_with("jesse smith", :any => true) but it works when I try tagged_with("jason stephan", :any => true) or tagged_with("black ink", :any => true)
So each term has a space in it but for whatever reason 'jesse smith' kills the action.
My console shows that I have a routing error too:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"tattoos", :member_id=>nil, :id=>#<Tattoo id: 170, description: "", status: "approved", member_id: nil, created_at: "2011-10-25 23:08:17", updated_at: "2011-11-17 16:56:55", file_file_name: "starry-eyed-rabid-squirrelweb.jpg", file_content_type: "image/jpeg", file_file_size: 294782, file_updated_at: "2011-10-25 23:08:17", album_id: nil, position: 116, favorite_count: 0, share_count: 1, file_remote_url: "http://www.jessesmithtattoos.com/wp-content/gallery...">}):
22: <ol class="small_tattoos">
23: <% #tattoos.each do |t| %>
24: <li>
25: <%= link_to image_tag(t.file.url(:tiny),:alt=>"#{t.style_list}, rtattoos, tattoos"), member_tattoo_path(t.member, t) %>
26: </li>
27: <% end %>
28: </ol>
app/views/index/show.html.erb:25:in `block (2 levels) in _app_views_index_show_html_erb___1839804211534816245_69842632179360__4333294961394575926'
app/views/index/show.html.erb:23:in `block in _app_views_index_show_html_erb___1839804211534816245_69842632179360__4333294961394575926'
app/views/index/show.html.erb:11:in `_app_views_index_show_html_erb___1839804211534816245_69842632179360__4333294961394575926'
So why does the one term cause a routing error and not the others?
I guess you have problem with path helper:
member_tattoo_path(t.member, t)
I see in your error description that
:member_id=>nil
So it turns out that tattoo tagged with jesse smith hasn't corresponding association with name 'member', and path helper, which need valid id, throws exception.