rails globalization "undefined method for translates" error - ruby-on-rails

after watching rails cast: https://www.youtube.com/watch?v=u6CMY3mUv90
i tried myself to globalize my rails project but i am getting "undefiend method for translates" error
gem 'globalize'
i did a bundle install in my bash terminal and also did bundle updates sucessfully
then i added "translates: title, :content" like below:
class Post < ActiveRecord::Base
belongs_to :admin
has_many :comments
rails_admin do
edit do
field :title
field :content, :ck_editor
field :link_title
field :link
field :admin
end
end
translates: title, :content
end
then i added
class TranslatePosts < ActiveRecord::Migration
def self.up
Post.create_translation_table!({
:title => :string,
:text => :text
}, {
:migrate_data => true
})
end
def self.down
Post.drop_translation_table! :migrate_data => true
end
end
AND i did rake db:migrate
but i am getting the erro still. any idea why it is not working?

Two things to be checked;
- It seems that you missed colon in the translated attributes :title.
- Restart rails server may be helpful.

Related

Getting ActionViewTemplate error in Heroku but not local rails environment

I am deploying to Heroku and keep getting an error when loading the form to create a new room
ActionView::Template::Error (undefined method 'attractions' for Room)
I have added :attractions in permitted params in the RoomsController
params.require(:room).permit(:attractions)
and this is what the attractions text field looks like in the form
<%= form_for #room, html: {multipart: true} do |f| %>
<label for="textarea2" style="font-size: 17px; color: #0089ec;">Neighborhood Attractions</label>
<%= f.text_area :attractions, autofocus: true, :placeholder => "What tourist attractions are in your city", :class => "materialize-textarea" %>
This is the migration created to add the attractions field
class AddAttractionsToRoom < ActiveRecord::Migration
def change
add_column :rooms, :attractions, :text
end
end
and finally in the show page I have this
<div class="col m8 offset-m1 s12">
<p class="grey-text"><%= #room.attractions %></p>
</div>
Does anybody know why this is happening? Thanks
UPDATE:
So i checked the logs and found the problem is that i made migration errors and rake was aborting before the attractions field is created.
Here is the mess i made:
first i add a city_limit boolean field
class AddAmenitiesToRoom < ActiveRecord::Migration
def change
add_column :rooms, :city_limit, :boolean
end
end
then for some reason I forgot I had created the city_limit boolean field and instead thought I had a city string field. So I thought I should rename it to city_limit with this migration
class ChangeColumnName < ActiveRecord::Migration
def change
change_column :rooms, :city, :city_limit, 'boolean USING CAST(test_type AS boolean)'
end
end
And finally I decided, you know what, I think I need a city string field after all for geolocation so added another migration
class AddAcityToRoom < ActiveRecord::Migration
def change
add_column :rooms, :city, :string
end
end
Embarrassing, I know. Staying up late coding away is never good. But the problem can be easily fixed by removing this migration,
class ChangeColumnName < ActiveRecord::Migration
def change
change_column :rooms, :city, :city_limit, 'boolean USING CAST(test_type AS boolean)'
end
end
I just dont know if deleting the migration file is a good idea since I have already run rake db:migrate and there are about 10 other migrations after this one so rake db:rollback is not really an option
Perhaps is a problem of static assets....
Try to install gem 'rails_12factor'
gem 'rails_12factor', group: :production
bundle install

facing issues in enum field in rails4

Hi I have generated a migration to add_column rails g migration AddColumnToEmployees
class AddColumnToEmployees < ActiveRecord::Migration
def change
add_column :employees, :role, "enum('SUPER-ADMIN', 'HR','ADMIN','INVENTORY','EMPLOYEE')", :default => 'EMPLOYEE'
end
end
run rake db:migrate
Now I want to access role in my view for this I have written this:
<%=f.select :role, :collection => Employee.roles %>
But its not accessing it. It gives error
undefined method 'roles' for #<Class:0xc2acd88>
Please guide how to solve this. Thanks in advance!
I was under the impression you represented the enum as an integer in the DB, so your migration should be:
class AddColumnToEmployees < ActiveRecord::Migration
def change
# Assuming Employee is the first value in your enum list
add_column :employees, :role, :integer, default: 0
end
end
and your select should be:
<%= f.select :role, :collection => Employee.roles.keys.to_a %>
See Saving enum from select in Rails 4.1
Your model:
class Employee
enum role: [:employee, :inventory, :admin, :hr, :superadmin]
end
Rails does automatically provide you with all potential values through a class method with the pluralized attribute name.
your migration is fine. after your migration to access it in view like that
<%=f.select :role, :collection => Employee.roles.keys.to_a %>
and define enum field in model employee.rb
enum role: {
super_admin: 1,
hr: 2,
admin: 3,
inventory: 4,
employee: 5
}
convert in enum role of model into hash. and assign the value. and run it.i will try my best hope it will help you!!
Try following code, I hope this will help you.
in AddColumnToEmployees migration file.
class AddColumnToEmployees < ActiveRecord::Migration
def change
add_column :employees, :role, :integer, default: 0
end
end
in Employee Model.
class Employee
enum role: [ :super_admin, :hr, :admin, :inventory, :employee ]
end
and finally in view file.
<%= f.select :role, options_for_select(Employee.roles.collect { |e| [e[0].humanize, e[0]] }) %>

Can i set a default value without the new and create action and directly edit it using edit and update actions?

I have a Users model and a Questions model. Each user has_one Question and each questions belongs_to a User. The Questions model has 3 columns - QuestionOne, QuestionTwo and QuestionThree - each is set to a default value of the string "TBD".
When the user creates an account and signs in I want to display on his profile the Questions and the responses = "TBD" and then using Best in place I want him to be able to edit his responses using the edit and update actions. But I cant retrieve the questions using user.question.questionone because they are sit to nil as there is no create or new action here. How do I go about this?
# user.rb
class User << ActiveRecord::Base
has_one :question
after_create :create_question
def create_question
question = self.build_question
question.question_one = question.question_two = question.question_three = "TBD"
question.save
end
end
When new user is created, it does not have associated question yet, so you need to build it somehow. Common way is using ActiveRecord callbacks before_create or after_create, something like
# user.rb
class User << ActiveRecord::Base
has_one :question
after_create :create_question
end
Here you go, you can write a migration that add default questions whenever the question is created,
class ChangeDefaultQuestions < ActiveRecord::Migration
def self.up
change_column :questions, :question_one, :string, :default => 'What is your name?'
change_column :questions, :question_two, :string, :default => 'How old are you?'
change_column :questions, :question_three, :string, :default => 'Where do you live'
# to add default questions to previously created questions
Questions.update_all({ :question_one => 'your qestion', :question_two => 'your question', :question_three => 'your question' })
end
def self.down
change_column :movies, :rating, :string, :default => nil
Questions.update_all({ :question_one => '', :question_two => '', :question_three => '' })
end
end
i am sure it would answer your question

globalize3 configuration I18n.locale variable

take a look into my model and my migration
i only have one attribute to test the globalize3 gem
class Car < ActiveRecord::Base
attr_accessible :name
translates :name
end
my migration looks like the following
class CreateCars < ActiveRecord::Migration
def up
create_table :cars do |t|
t.timestamps
end
Car.create_translation_table! :name => :string
end
def down
Car.drop_translation_table!
drop_table :cars
end
end
and i got the following error while trying to save new car details with the attribute name
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: locale
i think i am missing some declaration/configuration for globalize3 to access the I18n.locale variable.
btw i am using rails 3.2.3 and ruby 1.9.3p125
just found an workaround to my problem by following this Issue
class Car < ActiveRecord::Base
attr_accessible :name
translates :name
class Translation
attr_accessible :locale
end
end
Shouldn't this be like:
class Car < ActiveRecord::Base
attr_accessible :name, :translations_attributes
translates :name
end
See:
Rails 3.2.3: How to mass assign associated models?

multiple languages models

I've been asked to design a multiple language application and I need advice with which is the best approach with Rails.
Basically all the tables have some common fields that doesn't need to
be translated and some others that need translation.
thank you
For this purpose, will approach gem globalize3. Easy to use.
In your gemfile:
gem 'globalize'
Model:
class Article < ActiveRecord::Base
translates :title, :text
end
And migration:
class CreateArticles < ActiveRecord::Migration
def up
create_table :articles do |t|
t.timestamps
end
Article.create_translation_table! :title => :string, :text => :text
end
def down
drop_table :articles
Article.drop_translation_table!
end
end
And run
rake db:migrate

Resources