Rails full_messages missing label - ruby-on-rails

For some reason rails full_messages is missing labels and simply contains the error without any clue as to which field it relates to:
[3] pry(#<Spree::CheckoutController>)> #order.errors
=> #<ActiveModel::Errors:0x007fce1caa8218
#base=
#<Spree::Order id: 87, number: "R442456123", item_total: #<BigDecimal:7fce1c57a4a8,'0.6495E2',18(36)>, total: #<BigDecimal:7fce1c5cd0e0,'0.6495E2',18(27)>, state: "address", adjustment_total: #<BigDecimal:7fce1c5cd270,'0.0',9(27)>, user_id: 57, completed_at: nil, bill_address_id: nil, ship_address_id: nil, payment_total: #<BigDecimal:7fce1cb184a0,'0.0',9(27)>, shipping_method_id: nil, shipment_state: nil, payment_state: nil, email: "nick#1-night.co.nz", special_instructions: nil, created_at: "2014-10-10 09:47:21", updated_at: "2014-10-10 09:47:24", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: 57, channel: "spree", tax_total: #<BigDecimal:7fce1cb217d0,'0.0',9(27)>>,
#messages=
{:"bill_address.phone"=>["can't be blank"],
:"ship_address.phone"=>["can't be blank"]}>
[4] pry(#<Spree::CheckoutController>)> #order.errors.full_messages
=> ["can't be blank", "can't be blank"]
I have added the attributes in en.yml - How do I get full messages to contain the model name ie: ["Billing address phone can't be blank", "Shipping address phone can't be blank"]
Using the following:
gem 'rails', '4.0.2'
gem 'spree', '2.1.4'

For me this was an issue of having (for a reason long forgotten) the following in my en.yml.
errors:
format: "%{message}"
After removing it labels show correctly.

I think the way you added the attributes in en.yml file is wrong. You should do it this way:
en:
activerecord:
attributes:
spree/order/bill_address:
phone: Billing address phone
spree/order/ship_address:
phone: Shipping address phone

Related

Rails showing all data from database to index page how to stop it?

i am new in rails and i have generated a resource named a company name:string bio:text and ceo:string but my index view showing all the db records but i haven't done any inspet so why it showing all the data in index page?
it showing like this
[#<Company id: 4, name: "google", bio: "ad company", ceo: "sundar pichaif", created_at: "2021-03-15 12:43:18.821528000 +0000", updated_at: "2021-03-15 13:05:49.407986000 +0000">, #<Company id: 5, name: "basecamp", bio: "task management", ceo: "jason fried", created_at: "2021-03-15 13:27:58.781628000 +0000", updated_at: "2021-03-15 13:27:58.781628000 +0000">, #<Company id: 6, name: "Github", bio: "code", ceo: "Chris Wanstrath", created_at: "2021-03-15 13:30:15.510656000 +0000", updated_at: "2021-03-15 13:30:55.654182000 +0000">]
please help me out :(
open your index file where it shows the data from your database, please remove "=" for example: remove this <%= #companies %> and add like this <% #companies %>, the reason I am telling you to remove is that when you use "=" this sign with <%= ..... %>, it will show all the data in this erb brackets, so remove this and your issue will be resolve, Thanks

Ruby validation passes in console, fails in rake task

This is my EmailContact model:
class EmailContact < ActiveRecord::Base
validates :email, :presence => true, :email => true
end
I am using the ruby gem valid_email.
I run the following in my rails console, in the same environment as my rake task I will show later:
>> email_contact = EmailContact.new(:email => 'a253545#gmail.com')
>> email_contact.valid?
true
So, as you can see, in the rails console I am building an EmailContact and it is valid.
Then I run this in my rake task:
list_entity = {:branch=>"Nashua Branch-YMCA of Greater Nashua", :branch_id=>"485", :call_type=>nil, :client_id=>"2264", :client_name=>"YMCA of Greater Nashua", :date_of_birth=>nil, :email=>"a253545#gmail.com", :first_name=>"Sridhar", :last_name=>"Tipirneni", :list_entity_id=>"277795", :mem_id=>"4085008", :mem_unit_id=>"2138728", :member_id=>"0213262-01", :membership_type=>"Dual 2 Adult Family", :membership_type_id=>"5203", :most_recent_join_date=>nil, :old_membership_type=>nil, :phone_number=>"(970)456-1010", :primary_language=>"English", :termination_date=>nil, :termination_reason=>nil, :unit_id=>"0213262", :unit_type=>nil, :visits=>nil, :"#i:type"=>"c:NpsListEntityDto"}
email_contact = EmailContact.new(list_entity.except(:"#i:type"))
puts email_contact.valid?
This returns false. The only validation, at all, is the email. Why does this email validate successfully in my console but fail in my rake task?
FYI, when I remove :email => true from my EmailContact model and only validate the presence of an :email, they both work fine. So the issue is definitely within the :email => true piece of my validation, but I don't understand why it passes in one place and fails in another.
EDIT
In my console, my model looks like this when using the full list_entity:
#<EmailContact id: nil, branch: "Nashua Branch-YMCA of Greater Nashua", branch_id: 485, call_type: nil, client_id: 2264, client_name: "YMCA of Greater Nashua", date_of_birth: nil, email: "a253545#gmail.com", first_name: "Sridhar", last_name: "Tipirneni", list_entity_id: 277795, mem_id: "4085008", mem_unit_id: "2138728", member_id: "0213262-01", membership_type: "Dual 2 Adult Family", membership_type_id: 5203, most_recent_join_date: nil, old_membership_type: nil, phone_number: "(970)456-1010", primary_language: "English", termination_date: nil, termination_reason: nil, unit_id: "0213262", visits: nil, loaded_at: nil, failed_at: nil, unit_type: nil, created_at: nil, updated_at: nil, list_id: nil>
In my rake task, when I run email_contact.inspect, this is returned:
#<EmailContact id: nil, branch: "Nashua Branch-YMCA of Greater Nashua", branch_id: 485, call_type: nil, client_id: 2264, client_name: "YMCA of Greater Nashua", date_of_birth: nil, email: "a253545#gmail.com", first_name: "Sridhar", last_name: "Tipirneni", list_entity_id: 277795, mem_id: "4085008", mem_unit_id: "2138728", member_id: "0213262-01", membership_type: "Dual 2 Adult Family", membership_type_id: 5203, most_recent_join_date: nil, old_membership_type: nil, phone_number: "(970)456-1010", primary_language: "English", termination_date: nil, termination_reason: nil, unit_id: "0213262", visits: nil, loaded_at: nil, failed_at: nil, unit_type: nil, created_at: nil, updated_at: nil, list_id: nil>
As you can see, they are both the exact same - The console model is valid, the rake model is invalid.
EDIT 2
I am using the valid_email gem, mentioned above. Here is the filepath:
/Users/luigi/.rvm/gems/ruby-2.0.0-p247#hub/gems/valid_email-0.0.4/lib/valid_email/email_validator.rb
All of my other gems are stored here as well it seems like.
It may also be worth mentioning that I get this warning before the validation fails:
[deprecated] I18n.enforce_available_locales will default to true in
the future. If you really want to skip validation of your locale you
can set I18n.enforce_available_locales = false to avoid this message.
20 hours later, I found the issue.
Using savon, all of the strings returned in my hash were being converted to a datatype of Nori::StringWithAttributes. The encoding was the same (UTF-8), but the class was different.
Running email_contact.email = email_contact.email.to_s prior to checking if the model is valid solves the issue.

Rails to_json method: problems with unicode characters

I'm having troubles with unicode characters in my Rails app. The problem is that when I store the object with some string containing scandinavian characters, those characters then are not rendered correctly when using to_json method. Basically all those characters are rendered like this:
\"description\":\"\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\"
Here is example:
Item.create(:title => "Test title", :description => "With scandic characters öäöäö")
=> #<Item id: 198, title: "Test title", priority: nil, description: "With scandic characters öäöäö", bought_when: nil, warranty_until: nil, created_at: "2012-12-04 15:22:19", updated_at: "2012-12-04 15:22:19", user_id: nil, custom_id: nil, uuid: nil, position: 1, public: nil, secure_details: "", bookmark_id: nil, giveaway: nil, trash: false>
At this point the created object looks ok.
Item.last
returns
=> #<Item id: 198, title: "Test title", priority: nil, description: "With scandic characters \xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6", bought_when: nil, warranty_until: nil, created_at: "2012-12-04 15:22:19", updated_at: "2012-12-04 15:22:19", user_id: nil, custom_id: nil, uuid: nil, position: 1, public: nil, secure_details: "", bookmark_id: nil, giveaway: nil, trash: false>
This "With scandic characters \xC3\xB6\xC3\xA4\xC3\xB6\xC3\xA4\xC3\xB6" - does it still look ok? Or do I miss some proper encoding already at the item creation stage?
Anyway, now when call Item.last.to_json, I get this:
"description\":\"With scandic characters \\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\\ufffd\"
And at this point the string is definitely broken.
I'm lacking some knowledge about the proper characters encoding in Rails, so any hints would be appreciated. The item, however, looks fine when rendered in the browser - this happens only when doing a JSON response.
Ok, the issue was that MySQL db was not in unicode. This is fixed now by migrating to PosgreSQL, ensuring it is in unicode and using Taps gem to migrate the data. All according to this Railscast: http://railscasts.com/episodes/342-migrating-to-postgresql

Rspec new expectation syntax

I've the following rspec unit test:
require 'spec_helper'
describe Article do
describe ".recents" do
it "includes articles created less than one week ago" do
article = Article.create(created_at: Date.today - 1.week + 1.second)
expect(Article.recents).to eql([article])
end
it "excludes articles published at midnight one week ago" do
article = Article.create!(:created_at => Date.today - 1.week)
expect(Article.recents).to be_empty
end
end
end
and the Articlemodel:
class Article < ActiveRecord::Base
attr_accessible :description, :name, :price, :created_at
scope :recents, where('created_at <= ?', 1.week.ago)
end
when I run my tests I get:
1) Article.recents includes articles created less than one week ago
Failure/Error: expect(Article.recents).to eql([article])
expected: [#<Article id: 60, name: nil, description: nil, price: nil, created_at: "2012-11-14 00:00:01", updated_at: "2012-11-21 10:12:33", section_id: nil>]
got: [#<Article id: 60, name: nil, description: nil, price: nil, created_at: "2012-11-14 00:00:01", updated_at: "2012-11-21 10:12:33", section_id: nil>]
(compared using eql?)
Diff:#<ActiveRecord::Relation:0x007ff692bce158>.==([#<Article id: 60, name: nil, description: nil, price: nil, created_at: "2012-11-14 00:00:01", updated_at: "2012-11-21 10:12:33", section_id: nil>]) returned false even though the diff between #<ActiveRecord::Relation:0x007ff692bce158> and [#<Article id: 60, name: nil, description: nil, price: nil, created_at: "2012-11-14 00:00:01", updated_at: "2012-11-21 10:12:33", section_id: nil>] is empty. Check the implementation of #<ActiveRecord::Relation:0x007ff692bce158>.==.
# ./spec/models/article_spec.rb:7:in `block (3 levels) in <top (required)>'
Could someone please help me to figure out what's the error in my test?
It seems good for me.
You are comparing an activerecord relation (Article.recents) to an array ([article]), which is why the expectation is failing. (It looks like they are the same in the spec results because inspect converts the relation into an array before printing it out.)
Change your first expectation to this:
expect(Article.recents.to_a).to eql([article])

Rails Syntax for entering data in sqlite

I have been following this tutorial, http://ruby.railstutorial.org/chapters/modeling-users?version=3.2#top,
and I tried this in the rails console
User.new(name: "Michael Hartl", email: "mhart#example.com")
SyntaxError: compile error
but when I tried this it worked:
User.new(:name=> "Michael Hartl", :email=>"mhart#example.com")
=> #<User id: nil, name: "Michael Hartl", email: "mhart#example.com", created_at: nil, updated_at: nil>
I am using rails 3.2.1, which I guess this is the problem, but why change the syntax to something more complicated like, :<variable>=> instead of <variable>: ??
You didn't type it correctly.
User.new(name: "Michael Hartl", email: "mhart#example.com")

Resources