Simple form and carmen

774 views
Skip to first unread message

Aziz Bookwala

unread,
May 18, 2012, 12:26:06 PM5/18/12
to plataformate...@googlegroups.com
Hello All

Ive just started using simple form for a project. For one form, I need a dropdown of countries for which I am using the Carmen rails gem. I tried using the carmen helper for countries:

f.country_select :location, priority: %w(IN), prompt: 'Please select a country'

but this does not render the label for the field.

Using f.country_select :location, priority: %w(IN), prompt: 'Please select a country', label: "Country" doesnt work.

If I use f.input :location, and provide a collection, the priority field is not respected. 

Any ideas as to what I should be doing?

Carlos Antonio da Silva

unread,
May 18, 2012, 1:48:25 PM5/18/12
to plataformate...@googlegroups.com
Using f.country_select won't work with SimpleForm features, because it's your carmen-rails gem that provides this helper, not SimpleForm.

There's an example on how to use an input with country select plugin in readme:

    f.input :shipping_country, :priority => [ "Brazil" ], :collection => [ "Australia", "Brazil", "New Zealand"]

It should look pretty much the same, with the difference that your attribute does not contain "country" in it, so you have to give :as => :country as an option as well. Try this way and see if it works properly.

-- 
At.
Carlos Antonio

Aziz Bookwala

unread,
May 18, 2012, 2:40:47 PM5/18/12
to plataformate...@googlegroups.com
Thanks Carlos for the help, but, adding :as => :country raises an error:

wrong number of arguments (4 for 3)
The stack trace is here. The error originates in carmen, so im guessing simple form is passing some extra parameters to carmen. Any pointers?

Carlos Antonio da Silva

unread,
May 18, 2012, 2:56:42 PM5/18/12
to plataformate...@googlegroups.com
It is possible, because SimpleForm was only integrated with country_select plugin so far, and apparently their api is a bit different. That means SimpleForm doesn't know about Carmem capabilities. The best you could do would be to create a custom priority input to handle carmem requirements, you'll mainly need to reimplement this method: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/inputs/priority_input.rb#L4, inheriting from this input.

Take a look at the wiki, there are examples there to guide you if you have doubts with custom inputs, or feel free to ask any other questions.

-- 
At.
Carlos Antonio

azizmb.in

unread,
May 18, 2012, 3:29:31 PM5/18/12
to plataformate...@googlegroups.com
Thanks Carlos for the pointers. This seems to work. Im not sure if it breaks something else though. 

Also, the same carmen gem has another helper called subregion_select, which, accepts another parameter parent_region_or_code. If I want to make this available to the custom form input, do I have to pass it in the options hash, and extract it in the input method, or is there a cleaner approach?
--
- Aziz M. Bookwala

Carlos Antonio da Silva

unread,
May 18, 2012, 4:16:55 PM5/18/12
to plataformate...@googlegroups.com
You have to do this: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/form_builder.rb#L27, that registers the option you give as :as, to the input to be rendered.

Just call something like SimpleForm::FormBuilder.map_type :subregion, to: SubRegionInput, and define that class the same way you did the other. You could also use SimpleForm::Inputs::SubRegionInput if you want, and then create the custom input.. I think both would work.

Remember you have to use :as => :subregion.

ps: a quick hint, you don't need to use :send in your example.. Looks great ;)
ps2: if you get everything working, perhaps you could post new examples to SimpleForm wiki? Or even open a new page on how to integrate with carmen-rails or something, up to you :). Thanks!

-- 
At.
Carlos Antonio

azizmb.in

unread,
May 19, 2012, 1:06:00 AM5/19/12
to plataformate...@googlegroups.com
Hey Carlos

Will try it out. Thanks again for the help! 

Il add a page on the wiki once ive tested things out and am sure they work as expected.

Carlos Antonio da Silva

unread,
May 19, 2012, 7:16:15 AM5/19/12
to plataformate...@googlegroups.com
Great! Thanks.

-- 
At.
Carlos Antonio

y...@reverb.com

unread,
Jul 19, 2013, 2:05:20 PM7/19/13
to plataformate...@googlegroups.com
Hey guys, has anyone developed a solution they're willing to share?

thanks
Yan
Reply all
Reply to author
Forward
0 new messages