Rails: Undefined local variable or method with Country-State-Select gem - ruby-on-rails

I am currently working on a Rails application where I need to list countries and their corresponding states/provinces. For this function, I am using the Country-State-Select gem.
I have included the gem in my Gemfile
gem 'country_state_select'
And installed it
bundle install
However, the official documentation used simple_form objects while I am using the default rails form objects.
Here's my form field for country, which works very fine:
<%= form.label :country %>
<%= form.select :country_field, collection: CountryStateSelect.countries_collection %>
However, the form field for state which is below:
<%= form.label :state %>
<%= form.select :state_field, CountryStateSelect.state_options(form: form, field_names: { country: country_field, state: state_field } ) %>
keeps throwing errors:
undefined local variable or method `country_field'
Here's the simple form implementation of it in the Country-State-Select gem documentation, which I am trying to convert to the default rails form objects:
<%= options = { form: f, field_names: { :country => :country_field, :state => :state_field } }
f.input :state_field, CountryStateSelect.state_options(options) %>
I would appreciate some form of help. Thank you.

I just learnt from Arvind Vyas the owner of the Country-Select-Select gem in this Stackoverflow answer that the gem depends on the simple_form gem.
So the best bet is to integrate the simple_form gem into the application or look for other alternatives, since the default rails form which I am using will not work for the implementation.

Related

How to get multiple fields form into one Json column using ActiveAdmin Active Admin Ruby on Rails and MySQL?

Hi I'm new to ActiveAdmin, it's inherent Formtastic and Ruby (2.6) on Rails (6.1) and need to make a modification on an existing system.
To make it short, I have a Prices model, which is filled with data on a prices.html.erb form.
I need to add key/value pairs (eg: {"fee_1": 10, "fee_2": 20,...}) to be stored in a JSON "fees_list" column, in a MySQL database.
I'm having issues about how to modify the form to input the values.
Amongst many attempts so far, in the prices.html.erb Formtastic form I've tried (Edit: Thanks to Lam Phan for typo fix):
<%= f.inputs, for: => :fees_list do |j| %>
<%= j.input :fee_1, require: false, input_html: { value: resource.fees_list['fee_1'] } %>
<%= j.input :fee_2, require: false, input_html: { value: resource.fees_list['fee_2'] } %>
<%= end %>
Additional information is that I have a #prices reference available in the prices.html.erb file.
But the resulting page does not display the price inputs. Do I need some Gem to edit/show the JSON data in the DB?
Could someone help please? Thanks a lot to this great community in advance. Best regards!

Ransack search not working in rails model but working fine in rails console

I have a problem with Ransack. Here is my Search Form
= search_form_for #query, :url => search_products_path, :html => { :method => :get } do |f|
%dl.left
%dl.left
%dt= f.label :sku, t('shoppe.orders.sku')
%dd= f.text_field :with_sku
%dl.right
%dt= f.label :name, t('shoppe.products.name')
%dd= f.text_field :with_translated_name
and my model code for searching is
def self.with_sku(sku_string)
sku_string = sku_string.squish
where("LOWER(shoppe_products.sku) LIKE ?" , "%#{sku_string}%".downcase)
end
So. Problem is SKU search is not working when a number field is entered and converted to string but it is working fine when this is done in rails console. Any help would be loved :)
I could be wrong but I think squish is a string method. I get errors when trying to call it on an integer:
NoMethodError: undefined method `squish' for 245:Integer
Just to be safe, explicitly cast your search value to a String first, then it should work:
str = str.to_s.squish
It was actually a problem with some of my associations. I updated them and it is working fine.

How do I get simple_form and bootstrap to work together?

I'm using simple_form to generate my forms in my Rails application. I'm wanting to use bootstrap with these forms. So I've installed the following gems and added them to my gemfile... (1)simple_form, (2)twitter-bootstrap-rails. Finally, per instructions on the simple_form git page I ran, rails generate simple_form:install --bootstrap.
After running bundle install everything is installed and the server runs. The forms however are still not displaying correctly as they would with the class "form-control".
Form
= simple_form_for #user, html: { multipart: :true, class: "form-horizontal" } do |f|
= f.input :headshot_image, as: :file
= f.input :remote_headshot_image_url, placeholder: "Image Url"
= f.input :first_name, required: true
= f.input :middle_name
= f.input :last_name, required: true
I am able to get it to "work" by two different methods (neither of which I believe are correct). First I can add , class: "form-control" to each input, or I can add to the simple_form_for :defaults => { :input_html => { :class => "form-control" } }. The former which kinda defeats the purpose I believe, and the latter works, but it also applies the class to the file input which is not ideal.
Is there a setup step that I missed? Or have I done something incorrect. If I need to provide more information or left something out please let me know. Thanks!
Probably you forgot about adding bootstrap assets to application.css file
simple_form Readme has following line:
You have to be sure that you added a copy of the Bootstrap assets on your application.

Rails Simple Form Gem not setting association id

I'm trying to use the simple form gem to populate a select box using an association. I have two associated models: job_category and job_type.
job_category has_many job_types
and
job_type belongs to job_category.
I'm successfully populating the select box using this form,
= simple_form_for(#job_type) do |f|
= f.error_notification
.form-inputs
= f.input :name
= f.association :job_category, :include_blank => false, :label_method => :name
.form-actions
= f.button :submit
and it is creating a job_type object when I hit my create route, but the job_category_id field in job_type isn't being set.
When I go to the command line instead, I am able to successfully create a new job using
JobType.create(name: "Test", job_category_id: 1)
Inspecting the post using the Chrome developer tools shows job_category_id being sent over as part of the post, so I don't have any idea why this isn't working.
If you are using Rails 4, can you confirm that you've added :job_category_id to your job_type_params method in JobTypesController?
If you have not, make sure :job_category_id is in your job_type_params method.
If you're using < Rails 4, add :job_category_id to your attr_accessible.

To use the :country input, please install a country_select plugin

I have a country attribute to my guidelines model. I don't want to use a plugin, I just want to have country as a string. Everything is working until I try to edit a guideline in activeadmin and then I get the error message:
ActionView::Template::Error (To use the :country input, please install
a country_select plugin, like this one:
https://github.com/jamesds/country-select):
1: insert_tag renderer_for(:edit)
in my form I have
<%= f.input :country, as: :string%>
in my admin/guidelines.rb I have
index do
column :title
column :specialty
column :content
column :hospital
column :country
column :user
default_actions
end
I'm not sure where you get this form code from but I had the same issue with Active Admin and resolved it by explicitly instructing the form to treat the field as a string:
ActiveAdmin.register Guideline do
form do |f|
f.inputs 'Details' do
f.input :country, :as => :string
end
f.actions
end
end
First you need to add the gem in GemFile
gem 'country-select'
Create a helper file '/app/helpers/active_admin/views_helper.rb'. Add the below code
module ActiveAdmin::ViewsHelper
def country_dropdown
ActionView::Helpers::FormOptionsHelper::COUNTRIES
end
end
In your view file use
form do |f|
f.inputs do
f.input :country, as: :select, collection: country_dropdown
end
f.actions
end
Use country_select. Seems to work fine with Rails 4.1 if you're doing this recently. Plus Rails old repo links to this one rather than country-select.
You're using ActiveAdmin, so you're also using Formtastic.
In Formtastic, in the file formtastic/lib/formtastic/inputs/country_input.rb it clearly says:
# Rails doesn't come with a `country_select` helper by default any more, so you'll need to do
# one of the following:
#
# * install the [country_select](https://github.com/stefanpenner/country_select) gem
# * install any other country_select plugin that behaves in a similar way
# * roll your own `country_select` helper with the same args and options as the Rails one
I would add gem 'country-select' to your Gemfile and do a bundle install as is the simplest and fastest solution.
I guess you could install the gem, and then override the display in active_admin.
I recommend you to use the country-select:
gem 'country-select'
I spent a lot of hours to find out why country-select is not working in my form :)

Resources