I'm using rails' active admin and working on a create screen for a model that belongs_to a parent. I'm getting an unpermitted_param error because activeadmin is automatically adding the parent's id to the form's parameters to be submitted. However, the parameter is being added outside of the model's hash. Here are the params submitted:
{"utf8"=>"✓",
"authenticity_token"=>"9MBGkptu3jjLd4Zoy6lLUe1r6hW9TwRAmmiUNz2SwvQapRYv8nvOqZKWZKhgn9TEIXwNiD+IheQERVs+DpUgfA==",
"wedding_cake"=>{"client_id"=>"1", "first_name"=>"bob",
"last_name"=>"smith", "address"=>"11 big circle",
"city"=>"boston", "state"=>"ma ", "zip"=>"01234",
"phone"=>"1234567890", "email"=>"", "guests"=>"150",
"time"=>"12:30pm", "special_instructions"=>"", "package"=>"lake view
pavillion", "flower_price"=>"0", "rolled_chocolate_price"=>"0",
"other_price"=>"0", "tiering_price"=>"0", "deposit"=>"50",
"balance"=>"150", "vendor_balance"=>"0", "delivery_price"=>"0",
"consultant"=>"peter", "date(1i)"=>"2019", "date(2i)"=>"11",
"date(3i)"=>"18", "slice_price"=>"3.50",
"appointment_date(1i)"=>"2018", "appointment_date(2i)"=>"10",
"appointment_date(3i)"=>"16"}, "commit"=>"Create wedding cake",
"client_id"=>"1"}
wedding_cake belongs to client and should have a client_id (which it does). However, as you can see there is another client_id outside of wedding_cake. Here is the code I am using to generate the form:
form do |f|
inputs 'Venue Info' do
f.input :client_id, :as => :hidden, :input_html => { :value => f.object.client_id }
f.input :first_name
f.input :last_name
f.input :address
f.input :city
f.input :state
f.input :zip
f.input :phone
f.input :email
end
inputs 'Cake Info' do
f.input :guests
f.input :time
f.input :special_instructions
f.input :package
f.input :flower_price
f.input :rolled_chocolate_price
f.input :other_price
f.input :tiering_price
f.input :deposit
f.input :balance
f.input :vendor_balance
f.input :delivery_price
f.input :consultant
f.input :date
f.input :slice_price
f.input :appointment_date
end
f.submit
end
my strong_parameters are set up as such:
I have my permitted_params set as such in my activeadmin registration: permit_params :client_id, :base_price, :first_name, :last_name, :address, :city, :state, :zip, :phone, :email, :guests, :time, :special_instructions, :package, :flower_price, :rolled_chocolate_price, :other_price, :tiering_price, :deposit, :balance, :vendor_balance, :delivery_price, :consultant, :date, :slice_price, :appointment_date
Related
I'm creating an active admin form where I want to set the value of "admin_user_id" to be the current user logging in and want to hide this as we don't need to show while filling the form. here is my form for active admin:
form do |f|
f.inputs do
f.input :admin_user_id, input_html: { value: current_admin_user }
f.input :title
f.input :description
end
f.actions
end
I resolved the issue by replacing "current_admin_user" with "current_admin_user.id".
form do |f|
f.inputs do
f.input :admin_user_id, input_html: { value: current_admin_user.id }
f.input :title
f.input :description
end
f.actions
end
I am displaying a form using ruby on rails as follows.
form do |f|
f.semantic_errors *f.object.errors.keys
# Form creation
f.inputs "User" do
f.input :first_name
f.input :last_name
f.input :email
f.input :phone_number, required: false, as: :number
f.input :password
f.input :text_sms, as: :radio, :label => "Receive sms", :checked => "Yes"
end
f.action
end
This displays the form correctly, but the problem exists when I click the radio button. I am unable to select the radio button.
By default Yes is selected, but when I click on No, it doesn't allow me.
Any help will be appreciated.
Rails version : 4.2.0
There is way, Try to replace your with
f.input :text_sms, as: :radio, :label => "Receive sms",:collection => [ ['Yes','yes',{:checked => true}], ['No','no'] ]
As given below:
form do |f|
f.semantic_errors *f.object.errors.keys
# Form creation
f.inputs "User" do
f.input :first_name
f.input :last_name
f.input :email
f.input :phone_number, required: false, as: :number
f.input :password
f.input :text_sms, as: :radio, :label => "Receive sms",:collection => [ ['Yes','yes',{:checked => true}], ['No','no'] ]
end
f.action
end
I am current using activeadmin to create a nested form. I can click on the add nested resource button which will display the inputs for the respective nested resources. However, I would like to have the inputs display by default without having to click on the add resource button (i.e the inputs should be displayed once the form is loaded).
I have rechecked my code against the ActiveAdmin docs and also checked various stackoverflow posts but was unable to make much progress.
Picture of how the form current looks like
My code is as follows in admin/project.rb:
form title: 'Create a New Project' do |f|
f.inputs 'Basic Information' do
f.input :category
f.input :name
f.input :location
f.input :size, sortable: :size do |project|
"#{project.size}m2"
end
f.input :published?
f.has_many :project_main_image, heading: 'Project main image', allow_destroy: true, new_record: false do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
end
f.has_many :project_descriptions, allow_destroy: true do |a|
a.input :contents, as: :ckeditor, input_html: { ckeditor: { toolbar: 'Full' } }
end
f.has_many :project_gallery_images, allow_destroy: true do |a|
a.input :photo, hint: image_tag(a.object.photo.thumb.url, class: 'active-admin-thumbnail')
a.input :orientation
a.input :row_order
end
f.actions
end
end
Any feedback or advice. Do let me know if you require more information.
That's what something i did.
you can take nested attributes like this with for keyword:
form do |f|
f.inputs do
f.input :user, input_html: { disabled: true }
f.input :name
f.input :address
f.input :city
f.input :country, as: :string
end
f.buttons
f.inputs "Account Information", for: [:account, f.object.account] do |s|
s.input :active, as: :boolean
s.input :subscription, as: :boolean
s.input :expires_on, as: :datepicker
s.actions
end
end
controller do
def permitted_params
params.permit!
end
end
end
Hope this is helpful.
How can I use Active Admin's form block to update a table that tracks when a particular attribute in the current resource is updated?
For instance, I have something like this:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
f.input :new_comment
end
f.buttons
end
where everything but new_comment is an attribute that exists on the current resource.
I tried doing something like this in the class for the resource:
def new_comment
history = History.new(:id, Time.now)
history.comment
end
I also tried creating a controller instance variable when that failed, but that didn't work either. The venue class doesn't really have any co-relation to the history class. I'm just trying to create an input column that will create a new row in my History table whenever the user edits a venue and then adds a comment.
The History model looks like this:
class History < ActiveRecord::Base
attr_accessible :comment, :venue_id, :created_at
belongs_to :venue
def initialize(id, datetime)
#id = id
#datetime = datetime
end
end
The form is for a Venue that looks like this:
class Venue < ActiveRecord::Base
attr_accessible :name, :address, :zip, :city
has_many :histories
accepts_nested_attributes_for :histories
//with a bunch of methods
end
I tried doing this after some digging but it didn't work either:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
end
f.inputs "History" do
f.has_many :histories do |h|
h.input :id
h.input :comment
h.input :modified_at
end
end
f.buttons
end
In addition to setting up the model relationships and accepts_nested_attributes_for, try this in your ActiveAdmin resource:
form do |f|
f.inputs "Details" do
f.input :name
f.input :address
f.input :zip
f.input :city
f.input :active_state, as: :select, collection: active_states
f.input :approval_state, as: :select, collection: approval_states
f.has_many :histories do |h|
h.input :id
h.input :comment
h.input :modified_at
end
end
f.actions
end
I have a model relation of:
event -> has many schedules
schedule -> has one address
I wanted to have a extra textfield in the address form which allows user to enter an address and click 'search button', so that user can ajax select the geocoder addresses.
however, I can't seem to create an custom(non model related) textfield or button
form do |f|
f.inputs do
f.input :title
f.input :category
f.input :avatar, :as => :file
f.input :description, as: :html_editor
#schedules
f.has_many :schedules do |schedule|
schedule.inputs "Schedules" do
link_to("Auto Fill Address", find_location_path)
#address
schedule.inputs "Address", :for => [:address, schedule.object.address || Address.new] do |address|
address.input :full_address
address.input :city
address.input :country, :include_blank => true
address.input :postcode
address.input :latitude
address.input :longitude
end
#schedule times
schedule.input :start_time, :as => :just_datetime_picker
schedule.input :end_time, :as => :just_datetime_picker
end
end
end
f.buttons
end