Ruby Strange Error

Ruby Strange Error

Rubygems is installed with ruby 1.9 by default.

Check that the file you are trying to load is in a directory listed in the variable $: or specify the entire path to the file in the require. Or, add the directory to $: explicitly:

$: << '/my/lib/path'
require 'mylib'

Strange error while translating the word admin in Rails

This most likely has to do with a conflict with some other gem that adds an admin namespace, like maybe ActiveAdmin or rails_admin, or maybe a role authorization plugin. That's why it's returning a hash instead of a single string value.

Some other code or gem has loaded the admin key to look something like this:

en:
admin:
js:
'true': true
'false': false
is_present: Is present
....etc....

I would suggest nesting your key to avoid the conflict with something like:

en:
user_type:
admin: Administrator
helper.t('user_type.admin')

I would also suggest setting up the i18n-tasks gem, which can find and correct issues with translation files, as part of your CI or test suite, which I believe would have detected this conflict with the command i18n-tasks health and shown that admin is a dupilcate key, so when your yaml files and the gem's get combined, only the last one to write to the translations hash will be preserved.

Strange end-of-input error in Ruby

Here is the fix :

def is_a_prime(n)
2.upto(n) do |x|
return false if n % x == 0
end
true
end
puts is_a_prime(4)
# >> false

I'd write this prime number check code as :

def is_prime? n
2.upto(n-1) { |x| return false if n % x == 0 }
n > 1
end

is_prime? 5 # => true
is_prime? 4 # => false
is_prime? 7 # => true

Strange error with belongs_to association between Comment and User

The error suggests you have an issue with the association.


The quick fix would be to use try:

<%= comment.author.try(:full_name) %>

This is the equivalent of... <%= comment.author.full_name if comment.author %>

--

I'd personally use the following:

#app/models/user.rb
class User < ActiveRecord::Base
has_many :comments, foreign_key: :author_id
end

#app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :author, class_name: "User"
end

Strange syntax error unexpected unary+ related to a space after `+`

The unary +@ operator is a valid one on its own (takes a single operand and returns its value - eg. +4 returns 4). Not to be confused with the addition operator + which works on two operands.

Also, it takes precedence over the normal + addition operator.

Hence, +c in a + b +c in a + b +c is getting parsed as the equivalent of a + b c, which is not valid, as Ruby is unable to determine whether the +c is a normal method argument () or block {}.

Strange rails error

The "couldn't parse YAML" error suggests that there is an issue with the formatting of a YAML file.

In your case, there's a formatting error in your devise.en.yml:

not_saved:
one: "1 error prohibited this %{resource} from being saved:"
:"%{count} errors prohibited this %{resource} from being saved:"

Before the : on the last line, it is missing a key. Perhaps this was accidentally deleted somehow? In other devise translation files, I typically see the key "other".

not_saved:
one: "1 error prohibited this %{resource} from being saved:"
other: "%{count} errors prohibited this %{resource} from being saved:"

Getting strange error when searching by date

If both parameters are set at all times, you can use:

@financial_reports = current_user.financial_reports.where(:created_at => ((params[:start_date].to_date)..(params[:end_date].to_date))

If that's not the case, you could (for example) do this:

@financial_reports = current_user.financial_reports

if params[:start_date].present?
@financial_reports = current_user.financial_reports.where("created_at >= ?", params[:start_date])
end

if params[:end_date].present?
@financial_reports = current_user.financial_reports.where("created_at <= ?", params[:end_date])
end

You will probably want to encapsulate this in scopes.

Super Strange Ruby, or Rails, Non-Error

I have also run into this issue once without a 'real' solution, however I was able to work around it:

Change

product.test_field = "Made it" 

to

product.update_attributes!(test_field: "Made it")

And this should fix the issue. Although I agree with the comments that somebody should probably report it to the Rails team, I just don't have the motivation to actually do so.



Related Topics



Leave a reply



Submit