Rails 4.2.4 ActiveAdmin Globalize show one input only - ruby-on-rails

Here I have a problem when using rails 4.2.4 + activeadmin 0.6.6 + rails-i18n 4.0.8 + globalize 4.0.3 + activeadmin-globalize 1.0.0 .
The problem I am facing is shown in the following image, it only show one input box for me:
In the contact table, I should have more variables as below:
db/migrate/XXXXXXXXX_create_contacts.rb
class CreateContacts < ActiveRecord::Migration
def up
create_table :contacts do |t|
t.string :url
t.boolean :publish, :default => false
t.integer :sequence
t.timestamps null: false
end
end
def down
drop_table :contacts
end
end
db/migrate/XXXXXXXXX_translate_for_contacts.rb
class TranslateForContact < ActiveRecord::Migration
def up
Contact.create_translation_table! :tool => :string, :content => :text
end
def down
Contact.drop_translation_table!
end
end
Contact table is originally generated using scaffold.
:url, :publish, :sequence are the variables common in all locales.
Only :tool and :content need to translate.
In app/models/contact.rb
class Contact < ActiveRecord::Base
active_admin_translates :tool, :content do
validates_presence_of :tool, :content
end
translates :tool, :content
end
In app/admin/contact.rb
ActiveAdmin.register Contact do
permit_params :url, :tool, :content, :publish, :sequence, translations_attributes: [:id, :locale, :tool, :content]
index do
translation_status
default_actions
end
form do |f|
f.translated_inputs "Translated fields", switch_locale: false do |t|
t.input :tool
t.input :content
end
f.actions
end
end
One more related thing, as I have also facing the "missing form_buffers" problem, I have edited the code in the activeadmin-globalize gem as following webpage:
https://github.com/maxime-carbonneau/activeadmin-globalize/commit/734f375152982ccde12e7810760a7ab82c8d4a20
but I am not sure if this edit will cause the problem.
Before I install and use activeadmin-globalize, I am sure there are the input boxes for :url, :publish, :sequence.
Do anyone have the solution or know what's happened? Thanks!
----------------Final Solution--------------------
As activeadmin-globalize is not maintained, most of it function not work normally. I recommend using another gem for it.

In the docs for the activeadmin-globalize gem, the author on Dec 9, 2014 warned users that he's not maintaining the gem anymore and to take it as you will. You may want to consider dropping the gem.
But, as activeadmin is concerned, I believe the reason your not seeing any other form inputs on your page is because you have haven't included them in your code below
# app/admin/contact.rb
...
form do |f|
f.translated_inputs "Translated fields", switch_locale: false do |t|
t.input :tool
t.input :content
end
f.actions
end
If you want to include them back in you will either need to just remove that entire block of code altogether and let activeadmin create the default form inputs for you or you can individually add your inputs back in
# app/admin/contact.rb
...
form do |f|
f.translated_inputs "Translated fields", switch_locale: false do |t|
t.input :url
t.input :tool
t.input :content
t.input :publish
...
end
f.actions
end

Related

Ruby On Rails Change Column to Null, Not Work?

I command in Terminal to change on column in database to not null, but It seems not work.
rails g migration change_column_null :Speaker, :surname, false
I got a file ChangeColumnNull But inside, it is nothing.
class ChangeColumnNull < ActiveRecord::Migration
def change
end
end
Lecture Controller (Def Create):
class CplecturesController < ApplicationController
layout 'cp_layout'
def create
#lecture = Lecture.new(lecture_params)
#lecture.save
redirect_to #lecture
end
private
def lecture_params
params.require(:lecture).permit(:lecture_title, :lecture_day, :column, :start_time, :end_time, :registered_speakers, :guest_speakers, :description)
end
end
Forms
<%= form_for :lecture, url:lectures_path do |f| %>
<form>
<div class="form-group">
<%=label_tag "Lecture Title" %><br>
<%= f.text_field :lecture_title, :class => "form-control", :placeholder => "Example: Why is Wordpress the best?" %>
</div>
Erase that migration and write a blank migration with a better name and then fill it out by setting a default value. If you have a default value it will never be null.
rails g migration ChangeColumnOnTableName
Then inside that migration do the following:
change_column :name_of_table, :name_of_column, :data_type_of_column, :null => false
If you're only worried about it being null based on what a user enters, you could simply add a validation that requires it. In your model:
validates :name_of_column, presence: true
if you are using latest ruby (2.5+) Here is the migration script to change fields from NOT NULL to NULL
class ChangeEmailPasswordToNullableUsers < ActiveRecord::Migration[5.2]
def change
change_column_null :users, :email, true
change_column_null :users, :password, true
end
end

Sudden no method error in Active Admin?

I have activeadmin installed and working fine for a 'reviews' section of may app, allowing me to add individual reviews to various locations in which my business is based. I tried to add the identical set up but using a BusinessReviews model rather than Reviews (thus allowing me to add business reviews on the same basis)
Everything works fine until I go into active admin (log in and accessing the 'Business Reviews' panel is fine, until I try and actually add a business review. I then get the error:
NoMethodError in Admin::BusinessReviews#new
undefined method `business_profile_image' for #<BusinessReview:0x007f893fe853d0>
My model is as follows:
class BusinessReview < ActiveRecord::Base
belongs_to :location
has_many :images, :as => :referrer
accepts_nested_attributes_for :images, :allow_destroy => true
def thumbnail
if self.images.count > 0
self.images.last.avatar(:thumb)
else
nil
end
end
end
my_app/admin/business_review.rb is as follows:
ActiveAdmin.register BusinessReview do
index do
column :business_reviewer_name
column :business_review_content
column :business_reviewer_address
column :rating
column :location do |business_review|
business_review.location.name
end
column :business_profile_image do |business_review|
image_tag(business_review.business_profile_image) if business_review.business_profile_image.present?
end
actions
end
show do |business_review|
attributes_table do
row :business_reviewer_name
row :business_profile_image
row :business_review_content
row :business_reviewer_address
row :rating
row :location do
business_review.location.name
end
end
panel "Images" do
table_for business_review.images do
column {|img| img.currently_used }
column {|img| image_tag(img.avatar.url(:large)) }
end
end
active_admin_comments
end
permit_params [:id, :business_reviewer_name, :business_profile_image, :business_review_content, :business_reviewer_address, :rating, :location_id], images_attributes: [:id,:_destroy,:avatar,:usage_type, :currently_used]
form do |f|
f.inputs 'Details' do
f.input :business_reviewer_name
f.input :business_profile_image
f.input :business_review_content
f.input :business_reviewer_address
f.input :rating
f.input :location
end
f.inputs "images" do
f.has_many :images, :allow_destroy => true, :heading => 'Images', :new_record => true do |imgf|
imgf.input :currently_used
imgf.inputs "Attachment", :multipart => true do
imgf.input :avatar, :as => :file, :hint => imgf.object.avatar? \
? imgf.template.image_tag(imgf.object.avatar.url(:large))
: imgf.template.content_tag(:span, "no image yet")
end
end
end
f.actions
end
end
Relevant part of my schema:
create_table "business_reviews", force: true do |t|
t.text "business_reviewer_content"
t.string "business_reviewer_name"
t.string "business_reviewer_address"
t.float "rating"
t.string "profile_image"
t.datetime "created_at"
t.datetime "updated_at"
end
The routes appear to be there ok too?
batch_action_admin_business_reviews POST /admin/business_reviews/batch_action(.:format) admin/business_reviews#batch
_action
admin_business_reviews GET /admin/business_reviews(.:format) admin/business_reviews#index
POST /admin/business_reviews(.:format) admin/business_reviews#creat
e
new_admin_business_review GET /admin/business_reviews/new(.:format) admin/business_reviews#new
edit_admin_business_review GET /admin/business_reviews/:id/edit(.:format) admin/business_reviews#edit
admin_business_review GET /admin/business_reviews/:id(.:format) admin/business_reviews#show
PATCH /admin/business_reviews/:id(.:format) admin/business_reviews#updat
e
PUT /admin/business_reviews/:id(.:format) admin/business_reviews#updat
e
DELETE /admin/business_reviews/:id(.:format) admin/business_reviews#destr
oy
I just don't get it as the reviews one I set up works perfectly and is identical (apart from the not haveing business_ appended to it).
According to your schema there is no business_profile_image but just profile_image:
t.string "profile_image"
So either rename the column or use profile_image instead of business_profile_image.

Unsure how the ancestry gem for rails works

I am trying to use the ancestry gem which I found by watching an old Ryan Bates Ancestry video and so I set up my my stuff like so:
Migration:
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :comment
t.string :author
t.integer :post_id
t.string :ancestry
t.index :ancestry
t.timestamps
end
end
end
Model:
class Comment < ActiveRecord::Base
belongs_to :post
has_ancestry
validates :comment, presence: true, length: {minimum: 15}
validates :author, presence: true
end
** Controller new Action: **
def new
post = Post.find_by(id: params[:post_id])
post.comments.new(:ancenstry => params[:parent_id])
end
So I think I have set up everything correctly. But when I run the following test:
it "should create a nested comment for a post" do
posted_comment = FactoryGirl.create(:comment, post_id: #post.id)
post :create, :post_id => #post.id, comment: {author: #comment.author, comment: #comment.comment, parent_id: #comment.id}
json = JSON.parse(response.body).to_json
binding.pry
parse_json(json, 'comment').to_json.should have_json_path('id')
end
And inspect the json after the binding pry:
{
"comment":{
"id":9,
"post_id":6,
"author":"Adam",
"comment":"Some Really long Valid Comment length of Something.",
"ancestry":null
}
}
The ancestry section is null. I have even tried tried changing parent_id to ancestry but that doesn't help either. Does any know what I am doing wrong? or have any ideas?
post.comments.new(:ancenstry => params[:parent_id])
The key of your hash is misspelled.

Creating a Rails 3 HABTM with Active Admin throws a 'Can't mass-assign protected attributes:' error

I am a rails noob so the below is probably down to lack of understanding however I have been looking/reading all day and cannot seem to find the solution.
I have two models - project and technology :
Project :
class Project < ActiveRecord::Base
attr_accessible description, :name
has_and_belongs_to_many :technologies, :join_table => :projects_technologies
end
Technology :
class Technology < ActiveRecord::Base
attr_accessible :abbr, :description, :name
has_and_belongs_to_many :projects, :join_table => :projects_technologies
end
My Create_Projects_Technologies migration was as follows :
class CreateProjectsTechnologies < ActiveRecord::Migration
def self.up
create_table :projects_technologies, :id => false do |t|
t.references :project
t.references :technology
end
add_index :projects_technologies, [:project_id, :technology_id]
add_index :projects_technologies, [:technology_id, :project_id]
end
def self.down
drop_table :projects_technologies
end
end
I am then using Active Admin to create and edit Project models using the following form :
ActiveAdmin.register Project do
form do |f|
f.inputs "Project attributes" do
f.input :name
f.input :description
f.input :technologies, as: :check_boxes
end
f.buttons
end
end
This correctly shows all my technologies as check boxes however as soon as I submit the form I hit the following error which I have not been able to overcome :
ActiveModel::MassAssignmentSecurity::Error in Admin::ProjectsController#update
Can't mass-assign protected attributes: technology_ids
All help is very much appreciate :D
Simple add technology_ids to Project attr_accessible
attr_accessible :client, :description, :name, :technology_ids

Rails money gem and form builder

I'm having an issue with the forms and the money gem.
This is my problem:
I create a record which has an "amount" field (mapped to money object). Let's say I enter 10 (dollars).
The money gem converts it to 1000 (cents)
I edit the same record and the form pre-populates the amount field as 1000
If I save the record without changing anything, it will convert the 1000 (dollars) to 100000 (cents)
How do I make it display the pre-populated amount in dollars instead of cents?
Edit:
I tried editing the _form.html like this:
= f.text_field(:amount, :to_money)
and I get this error:
undefined method `merge' for :to_money:Symbol
Given a migration as follows:
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.integer :cents
t.string :currency
t.timestamps
end
end
def self.down
drop_table :items
end
end
And a model as follows:
class Item < ActiveRecord::Base
composed_of :amount,
:class_name => "Money",
:mapping => [%w(cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't conver #{value.class} to Money") }
end
Then this form code should work perfectly (I just tested under Rails 3.0.3), properly displaying and saving the dollar amount every time you save/edit. (This is using the default scaffold update/create methods).
<%= form_for(#item) do |f| %>
<div class="field">
<%= f.label :amount %><br />
<%= f.text_field :amount %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You can now edit monetized fields directly (money-rails 1.3.0):
# add migration
add_column :products, :price, :price_cents
# set monetize for this field inside the model
class Product
monetize :price_cents
end
# inside form use .price instead of .price_cents method
f.text_field :price
See https://stackoverflow.com/a/30763084/46039
If you have multiple money fields in your table and you can't name them all "cents".
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.integer :purchase_price_cents
t.string :currency
t.timestamps
end
end
def self.down
drop_table :items
end
end
which would change your model to
class Item < ActiveRecord::Base
composed_of :purchase_price,
:class_name => "Money",
:mapping => [%w(purchase_price_cents cents), %w(currency currency_as_string)],
:constructor => Proc.new { |purchase_price_cents, currency| Money.new(purchase_price_cents || 0, currency || Money.default_currency) },
:converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
end
monetizing and the simple form, the steps as follows:
Migration
add_monetize :table, :amount
Model with validation
monetize :amount_cents, allow_nil: true, numericality: {greater_than: 0}
Controller permit params (dont use amount_cents here)
params.require(:model).permit(:amount)
simple form input
when it's saved it will be saved in cents in amount_cents column in db

Resources