Activeadmin ajaxing form with geocoder - ruby-on-rails

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

Related

How to fix Country-State-Select gem undefined method issue?

In a implementation of gem: https://github.com/arvindvyas/Country-State-Select
When i go to Post Job new page show this error on the log
undefined method `country' for #<Job:0x007f8c07092320>
I've tried to put the
accepts_nested_attributes_for
in the job model, but did not worked.
Someone have any hint to spare?
Job Model
class Job < ActiveRecord::Base
belongs_to :user
belongs_to :location
accepts_nested_attributes_for :location, allow_destroy: true
default_scope -> { order(created_at: :desc) }
end
Location Model
class Location < ActiveRecord::Base
has_many :users
has_many :jobs
end
Job new view
<%= simple_form_for #job do |f| %>
<%= f.simple_fields_for :location do |l| %>
<%= l.input :country, label: "Country", collection: CountryStateSelect.countries_collection %>
<%= l.input :state, CountryStateSelect.state_options(label: "State / Province", form: f, field_names: { :country => :country, :state => :state } ) %>
<%= l.input :city, CountryStateSelect.city_options(label: "City ", form: f, field_names: { :state => :state, :city => :city } ) %>
<% end %>
<%= f.input :name, label: 'Name', error: 'Name is mandatory' %>
<%= f.input :description, placeholder: 'user#domain.com' %>
<%= f.button :submit %>
<% end %>
In the fields
<%= l.input :state, CountryStateSelect.state_options(label: "State / Province", form: f, field_names: { :country => :country, :state => :state } ) %>
<%= l.input :city, CountryStateSelect.city_options(label: "City ", form: f, field_names: { :state => :state, :city => :city } ) %>
you have form: f, but you are within a simple_fields_for block which is currently using l instead of f
Looks like you're missing a country field in your Job model. Add it using a migration:
add_column :jobs, :country, :string

Nested Form does not Display form for ActiveAdmin

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.

active admin adding paren't id automatically

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

NoMethodError on a form create method

I am getting a weird error after adding <%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %> inorder to add a datetime selector.
I am wondering, what could the problem be?
NoMethodError in Messages#new
and
undefined method ``schedule' for #<Message:0x000000066fa1e8>
my create controller is
def create
#lists = current_user.lists.all
#message = Message.new(params[:message])
lists = params[:message][:lists]
schedule = params[:message][:schedule]
if #message.save
if schedule == [""]
MessageWorker.perform_async(#message.id, lists, current_user.id)
else
Schedule.create(execution_time: schedule, lists: lists, message_id: #message_id, user_id: current_user.id)
end
else
render action: "new"
flash[:notice] = "Messages Not Sent"
end
end
and the view is;
<%= simple_form_for(#message, :html => {:multipart => true}) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :from, placeholder: "Max 11 characters" , input_html: {id: 'sms_from'} %>
<%= f.input :to, :input_html => {:value => params[:to], :class => "to"}, placeholder: "Separate Numbers with Comma", hint: "Include atleast one number in this section. Separate the numbers with commas." %>
<div id="add_lists"> <span class="button"> Add List </span></div>
<div id="all_lists"> <%= f.collection_check_boxes :lists, #lists, :id, :name, as: :select %></div>
<%= f.input :message, :as => :text, :input_html => {:cols => 60, :rows => 7, id: "compose_message", placeholder: 'Type your message here!'} %>
<div>
<p>
<span id="char_count">160 characters left</span>
<span id="number_of_messages">1 message(s)</span>
</p>
</div>
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
<%= f.input :user_id, :as => :hidden, :input_html => {:value => #current_user.id} %>
<%= f.input :status, :as => :hidden, :input_html => {:value => "Queued"} %>
<%= f.button :submit, "Send Message" %>
<% end %>
and the model top looks like this;
class Message < ActiveRecord::Base
attr_accessible :message, :phone, :status, :to, :from, :user_id, :schedule
validates :message, :presence => true
validates :from, :presence => true
validates :to, :presence => true
validates :status, :presence => true
validates_length_of :message, :maximum => 1600, :allow_blank => true
validates_length_of :from, :maximum => 11, :allow_blank => false
belongs_to :user
Everything was working well until i added;
<%= f.input :schedule, as: :datetime, input_html: {id: 'schedule'} %>
Did you migrate the database to add the schedule column?
Or if you are not storing :schedule in the database, then you need to add:
attr_accessor :schedule
to your Message model.
You don't have to use f.input, you can use f.date_field of f.datetime_field as outlined in the API: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-date_field
Also, you don't have to use the form helper in this way as you can use datetimefield_tag instead of f.datetime_field as outlined here http://rubydoc.info/docs/rails/ActionView/Helpers/FormTagHelper:datetime_field_tag

upload multiple images at a time with paperclip using activeadmin

i'm trying to allow myself to upload 5 images for each model using active admin but i can't seem to figure out how to do it. here is my activeadmin code so far:
ActiveAdmin.register Piece do
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs "Details" do
f.input :name
f.input :description
f.input :cost
f.input :category
f.input :photo, :as => :file, :hint => f.template.image_tag(f.object.photo.url(:medium))
end
f.buttons
end
index do
column :id
column :name
column :cost
column :category
column :inventory_count
column :available_count
column :materials
column :created_at
default_actions
end
end
how would I allow for 5 at a time instead of just 1?
Here's how it'd work:
class Piece
has_many :pictures
accepts_nested_attributes_for :pictures
end
class Picture
has_attached_file :photo
end
Then in your active admin form, you'd
f.has_many :pictures do |ff|
ff.input :photo, as: :file, :hint => ff.template.image_tag(ff.object.photo.thumb.url)
ff.input :_destroy, as: :boolean
end

Resources