I added two email fields to my Restaurant table:
class AddEmailsToRestaurants < ActiveRecord::Migration[5.1]
def change
add_column :restaurants, :email2, :string, null: true, default: nil
add_column :restaurants, :email3, :string, null: true, default: nil
end
end
I ran rails db:migrate and I want to add these two fields in the Active Admin form in order to create new Restaurants from the admin interface:
ActiveAdmin.register Restaurant do
permit_params :email, :email2, :email3, [...]
filter :name
form do |f|
f.inputs "Restaurant" do
f.input :name
f.input :email
f.input :email2
f.input :email3
f.input :password
f.input :password_confirmation
f.input :address
f.input :address2
f.input :phone_number
f.input :city
f.input :post_code
f.input :latitude
f.input :longitude
f.input :photo
end
f.actions
end
[...]
end
But then when I click on 'Create new Restaurant' in my Admin interface, I get the following error: NoMethodError in Admin::Restaurants#new undefined method 'email2' for #<Restaurant:0x007f9490cc0340>
How can I solve this ?
Note that a running development environment will not detect changes to a table structure after a migration.
If you add columns via a migration, be sure to stop and restart your development server.
Related
I have this enum in the user model
enum role: { Principal: 0, Teacher: 1 }
In the active admin, in the create user form, the input to insert the role gives me the option of inserting an empty string.
How can I make it that it only gives me the options Principal and Teacher?
ActiveAdmin.register User do
actions :all, except: [:edit]
permit_params :email, :name, :password, :password_confirmation, :role
form do |f|
f.inputs do
f.input :email
f.input :name
f.input :password
f.input :password_confirmation
f.input :role
end
f.actions
end
end
For this you can pass a include_blank: false option
Means according to your code :-
....
....
f.input :role, include_blank: false
....
....
ActiveAdmin.register Type do
permit_params :name, :job, version_ids: []
index do
id_column
column :name
column :job
column :versions do |type|
type.versions.collect(&:name).join(', ')
end
actions
end
form(html: { multipart: true }) do |f|
f.inputs do
f.input :name, as: :string
f.input :job, as: :string
f.input :versions, as: :select, input_html: { multiple: true }
end
f.actions
end
end
This table has a has_many relation with workers table. The dropdown which shows the worker filter shows the worker object instead of the worker name. How can I get the worker name in the dropdown instead of the worker object.
Try:
f.input :versions, as: :select,
collection: versions.pluck(:name, :id),
input_html: { multiple: true }
I am using ActiveAdmin as a admin panel, so I can create users through ActiveAdmin.
The Issue I'm having is when updating or inserting a users password, I need the value I entered in the ActiveAdmin form to hash the password with bcrypt and then work with rails has_secure_password authentication
Is there anyway I can get ActiveAdmin to include something like this?
BCrypt::Password.create(params[:password])
before saving to the database?
this is my users.rb
ActiveAdmin.register User do
permit_params :email, :password_digest, :session_token, :session_key,
:rank, :profileColour
index do
selectable_column
id_column
column :email
column :password_digest
column :session_token
column :session_key
column :rank
column :profileColour
actions
end
filter :email
filter :session_token
filter :session_key
filter :rank
filter :profileColour
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :password_digest
f.input :rank
end
f.actions
end
end
Any advice would be greatly appreciated
Thanks!
Try to use :password instead of :password_digest
form do |f|
f.inputs "Admin Details" do
f.input :email
f.input :password
# f.input :password_confirmation
f.input :rank
end
f.actions
end
Don't forget to permit params
ActiveAdmin.register User do
permit_params :email, :password, :rank
end
Hi i have an admin page using active admin gem.
thing is while creating a new page i should be able to input name, amount and interval..But while updating only name field must show..other 2 values shouldnt be updated. This is my active admin file. How to make this happen. Thanks in advance
ActiveAdmin.register SubscriptionPlan do
menu priority: 10
permit_params :name, :amount, :interval
index do
selectable_column
default_actions
column :name
column :amount
column :interval
end
form do |f|
f.inputs "Subscription Plan" do
f.input :name
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
f.actions
end
end
Try this
form do |f|
f.inputs "Subscription Plan" do
f.input :name
if f.object.new_record?
f.input :amount
f.input :interval, as: :select, collection:["week","month","year"]
end
end
f.actions
end
I am using active admin and seem to be struggling with creating a record with an association. I have set this up in the normal way
class Membership < ActiveRecord::Base
belongs_to :member
attr_accessible :membership_type
end
class Member < ActiveRecord::Base
has_one :membership
accepts_nested_attributes_for :membership
attr_accessible :membership_attributes, :forename, :middlename, :surname, :house_no, :house_name, :street, :town, :postcode, :home_tel, :mobile_tel, :work_tel, :email
end
I then want to create a New member along with their membership type in the same form, so my member.rb looks like this so far
ActiveAdmin.register Member do
# Set Which Columns are to be displayed on the index page
index do
column :forename
column :middlename
column :surname
column :house_no
column :house_name
column :street
column :town
column :postcode
column :home_tel
column :mobile_tel
column :work_tel
column :email
column :membership do |member|
member.membership.map{ |ms| ms.membership_type}
end
default_actions
end
# Set Which Columns are to be displayed on Create New Member
form do |f|
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
f.input :house_no
f.input :house_name
f.input :street
f.input :town
f.input :postcode
f.input :home_tel
f.input :mobile_tel
f.input :work_tel
f.input :email
end
f.inputs :for => [:membership, f.object.membership || Membership.new] do |m|
m.input :membership_type, :label => 'Membership Type', :as => :select, :collection => Membership.all.map{|m| [m.membership_type]}
end
f.actions
end
end
This displays a select box where i can choose from various Membership types, but i dont seem to be passing the correct value when creating the record,
This is an example of what is being posted
member[forename]:Name 1
member[middlename]:Name 2
member[surname]:Name 3
member[house_no]:1
member[house_name]:
member[street]:Test Street
member[town]:Test Town
member[postcode]:CF23 7BD
member[home_tel]:01633222222
member[mobile_tel]:07864582356
member[work_tel]:01633555555
member[email]:test#mail.com
membership_attributes"=>{"membership_type"=>"Student"}
commit:Create Member
This doesn't seem right does it? Also when i try and view the record
column :memberships do |member|
member.memberships.map{ |ms| ms.membership_type}
end
I get the following mysql error
Mysql2::Error: Unknown column 'memberships.member_id' in 'where clause': SELECT `memberships`.* FROM `memberships` WHERE `memberships`.`member_id` = 10
Could anyone point me in the right direction please or can anyone see where im going wrong?
Much appreciated
Instead of f.input :memberships try this:
f.has_many :memberships do |pf|
pf.input :membership_type
end
and close your member object fields like this
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
...
end
So your form should look like this:
form do |f|
f.inputs "Member Registration" do
f.input :forename
f.input :middlename
f.input :surname
# .. the rest of your fields
end
f.has_many :memberships do |pf|
pf.input :membership_type
end
f.actions
end
Regarding the MySQL error, i think for the association to work you need to add the column member_id to the memberships table. You can create the migration with
rails g migration add_member_id_to_memberships member_id:integer
Then migrate with
bundle exec rake db:migrate