carmen-ruby / carmen-rails

NOT ACTIVELY MAINTAINED Rails adapter for Carmen (provides country_select and subregion_select)
MIT License
215 stars 159 forks source link

Regression with country_select #11

Open tmaier opened 12 years ago

tmaier commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

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 commented 12 years ago

@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 commented 12 years ago

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 commented 12 years ago

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 notifications@github.com 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 commented 12 years ago

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

kevinwmerritt commented 12 years ago

@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 commented 12 years ago

@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 commented 11 years ago

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!