Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression with country_select #11

Open
tmaier opened this issue Jul 10, 2012 · 9 comments
Open

Regression with country_select #11

tmaier opened this issue Jul 10, 2012 · 9 comments

Comments

@tmaier
Copy link

tmaier commented Jul 10, 2012

country_select has been a replacement for https://github.com/rails/country_select and therefore API compatible.

When I use it now - together with simple_form gem - I get the following error message:
wrong number of arguments (4 for 3)

@kevinwmerritt
Copy link
Contributor

I am also getting the same error with simple_form. @tmaier any idea on how to fix or a workaround?

<%= simple_form_for @organization  do |f| %>
  <%= f.input :name, :autofocus => true %>
  <%= f.input :country_code, :as => :country %>
<% end %>

@Choppa
Copy link

Choppa commented Aug 13, 2012

Same error here. In my case the error occured while using ActiveAdmin, which uses Formtastic for its forms. @kevinwmerritt: As a quick and dirty workaround the signature of the country_select method in the FormBuilder class (line 176) can be changed fromdef country_select(method, options = {}, html_options = {}) to
def country_select(method, priority_countries = nil, options = {}, html_options = {})

@neilsmind
Copy link

@Choppa I had the exact same problem regarding ActiveAdmin and based on this article: http://justinfrench.com/notebook/formtastic-2-preview-custom-inputs, I was able to create the following file and all seems good with the world:
https://gist.github.com/3485047

@jim
Copy link
Collaborator

jim commented Aug 27, 2012

Hi-

Can you try to use the latest version of master and see if this fixes the issue you are having? There was a commit a week or so ago that was supposed to fix this issue.

If that doesn't fix it, post back here and I'll see what I can do.

@neilsmind
Copy link

Hi Jim.
Yep. I'll give it a go today. Thanks for such a great plugin!

Regards, Neil

Sent from my iPhone

On Aug 26, 2012, at 10:21 PM, Jim Benton [email protected] wrote:

Hi-

Can you try to use the latest version of master and see if this fixes the issue you are having? There was a commit a week or so ago that was supposed to fix this issue.

If that doesn't fix it, post back here and I'll see what I can do.


Reply to this email directly or view it on GitHub.

@neilsmind
Copy link

Hi Jim.
Works great! Thank you. I'll let you know if I run into any issues.

@kevinwmerritt
Copy link
Contributor

@jim Thanks for putting together this gem. I'm using simple form and the following now works:

<%= f.input :country_code, :label => 'Country' %>

However, I am also using the nested_form gem and creating nested associations that include the country_code field and require the select helper.

While the country select options are generated for each nested association added, the persisted country_code attribute is not preselected in the list. So when the attribute = 'US', 'Afghanistan' always appears because it is the first country in the sorted list.

My quick fix for the nested country code fields is this:

<%= f.fields_for :sessions do |session| %>
  <%= session.input :country_code, :collection => Organization.region_options_for_select(Carmen::Country.all), :label_method => lambda { |i| i[0] }, :value_method => lambda { |i| i[1] } %>
  <%= session.link_to_remove "Remove this session" %>
<% end %>
<p><%= f.link_to_add "Add a session", :sessions %></p>
def self.region_options_for_select(regions, selected=nil, options={})
  options.stringify_keys!
  priority_region_codes = options['priority'] || []
  region_options = ""

  unless priority_region_codes.empty?
    unless regions.respond_to?(:coded)
      regions = Carmen::RegionCollection.new(regions)
    end

    priority_regions = priority_region_codes.map do |code|
      region = regions.coded(code)
      [region.name, region.code] if region
    end.compact
    unless priority_regions.empty?
      region_options += options_for_select(priority_regions, selected)
      region_options += "<option disabled>-------------</option>"
    end
  end

  main_options = regions.map { |r| [r.name, r.code] }
  main_options.sort!{|a, b| a.first.to_s <=> b.first.to_s}
end

I'm not sure if this problem can be addressed in this gem or in nested_form.

@jim
Copy link
Collaborator

jim commented Aug 29, 2012

@kevinwmerritt I haven't used nested_forms, but I looked through its code a bit. Nothing lept out at me that would cause this to not work.

If you create a form for a session object outside a nested_form, does the same problem occur?

@ianneub
Copy link

ianneub commented May 14, 2013

I ran into this issue today as well.

Turns out that it still wouldn't work with nested_form, but the "selected" option in the git version was able to workaround the issue.

Thanks for this gem btw. It's awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants