rails carmen country_select helper without a form object - ruby-on-rails

my form is not tied to a particular model and looks like this:
<%= form_tag(:controller => 'orders' , :action => 'process_credit_card') do %>
... bunch of fields ...
<% end %>
carmen-rails' country_select helper looks like this
<%= f.country_select :country_code, {priority: %w(US CA)}, prompt: 'Please select a country' %>
however I do not have a form object f, I use helpers like <%= text_field_tag 'billing_address[phone]' %> to create my form, is there a way I can still use carmen in this form?
UPDATE: I am using ActiveMerchant for payment processing, I can create a form with form_for instead of form_tag but I don't know how, any pointers will be appreciated.

this should work...
<%= country_select :country_code, {priority: %w(US CA)}, prompt: 'Select' %>

I'm a few years late, but I ran into this problem tonight and found a solution that worked for me by crawling through the carmen-rails source code. Note that the second hash (as the fourth argument) can contain HTML options like class.
<%= country_select(nil, :country_code, { priority: %w(US CA) }, {}) %>
With the other solutions (that did not have nil as the first argument) I was having serialization issues with my AJAX search functionality. The entire hash (including priority and other code) would be serialized with the request. When calling it like I have above, this is no longer an issue and only the country code is serialized. Hopefully this will help someone in the future who ran into the same issue we did.

Please use
<%= country_select(nil,:country, { priority: %w(US CA) , prompt: 'Select Country'},:class=>"form-control") %>
Use above with form_tag
<%= f.country_select :country, {priority: %w(US CA), prompt: 'Select Country'},:class=>"form-control" %>
and use above with form_for

The correct way to do this isn't by using the country_select gem but a dependency it has countries gem and plain Rails form helpers select_tag and options_for_select: Using country_select gem with form_tag and no model

Try:
<%= country_select_tag :country_code, {priority: %w(US CA)}, prompt: 'Please select a country' %>

Related

Ruby on Rails 2 Drop Down Menus Want Validation of Selection Pre-Controller

I have a ruby on rails app that has a form. I was wondering if there is a way to make sure that a user has selected drop down menu items in both of the drop downs on this form before it is submitted and params are generated. Ideally I would like to throw and error warning them to pick 1 item on each of the drop downs and re-render the form.
The form is below:
<%= form_tag compare_products_path, method: :get do |f| %>
<%= select_tag :product_id1, options_from_collection_for_select(#products, 'id', 'name') %>
<%= select_tag :product_id2, options_from_collection_for_select(#products, 'id', 'name') %>
<%= f.submit %>
<% end %>
Please let me know how I can accomplish what I stated above.
SIDENOTE: I also implemented Select2 to make the form look nicer but could not find out of there is a quick validation trick in Select2 to accomplish what I said above, if there is a suggestion for that I can post the Select2 version,
Try this:
<%= select_tag :product_id1, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select...",:required=>true %>
<%= select_tag :product_id2, options_from_collection_for_select(#products.where.not(:name=>nil), 'id', 'name'), :include_blank => "Please select..." ,:required=>true %>

Displaying countries using country_select gem in Rails 4

I am trying to implement the country_select gem (gem 'country_select','~> 2.1.0') in a Rails 4 app without success. This is the code in my form. I have tried several permutations of this from similar questions on stack without success. I don't understand why I am getting the error -
"undefined method `input' for ActionView::Helpers::FormBuilder:0x007fafb5878c48>"
Bear in mind that I am a Rails beginner.
<%= form_for(#user, html: { multipart: true }) do |f| %>
<%= f.label :country %><br>
<%= f.input :country, as: :country %>
<%= f.submit "Update my account", class: "btn btn-primary" %>
<% end %>
I have also tried
<%= f.label :country %>
<%= f.country_select :country %>
from the documentation. 'country' is one of my user attributes.
Any guidance would be welcome on the newbie problem.
I have just spent an hour on the same problem and am annoyed at the brevity of the github documentation. Nevertheless, this worked for me and it sounds like what you need:
<%= f.country_select(:country, {selected: #user.country}, {class: "form-control"}) %>
I couldn't get the second parameter (see https://github.com/stefanpenner/country_select) to work - it gave me an error every time I tried to add anything else before the {selected...}.
<%= f.country_select :country, {priority_countries: ["SG", "US"], include_blank: "select country"}, class: 'form-control' %>
This worked for me, the first :country is an attribute of user, all the options of country_select gem will come as the next parameter and the last param will take the class name and other format features.
If the param contains a single field it can be passed with or without brackets.

country_select not saving to database

I'm rendering this code from a partial:
<%= f.country_select :country_code, {object: f.object, prompt: "Country"}, required: true %>
And my country code isn't saving to the database. What am I missing?
I don't have enough rep points to just make a comment, but did you add country_code to the controller_params in controller?
And also, should it be nested and added to permitted _params?

How to sort alphabetically for country_select method in carmen-rails 1.0.0.beta 3

I am using carmen-rails 1.0.0.beta 3 and for this I have used the following code:
<%= form_for(contact, remote: true, html: {class: 'popup-form'}) do |f| %>
<%= f.label :country, t('country') %>
<%= f.country_select :country, include_blank: "Select" %>
<% end %>
This gives a result like this:
Aland Island,
Antartica,
Afghanisthan,
..........
But I want Afghanisthan to start the list in case of alphabetical order.
Can anyone give a solution to this sorting problem. It would be helpful.
Thanks.
They've fixed this sorting bug, but perhaps they've not repackaged the gem or something. Try specifying the Git URL in your gemfile and I think it will work for you.
gem 'carmen-rails', git: 'https://github.com/jim/carmen-rails.git'

Rails: submit (via AJAX) when drop-down option clicked

What is the easiest and most graceful way to auto-submit an AJAX form when a drop-down box's option is selected? I'm creating an administration page where the admin can modify user permissions (where "permissions" is stored as an integer), and I would like the "permissions" field to be a drop-down box that automatically submits and updates when the administrator clicks on the option he wants the user to have.
Here's the stripped-down version of what I'm currently looking at. I need to know the best way to convert this to a remote form that automatically submits when an option is clicked on.
Feel free to point out any tangential suggestions or anything else...I'm relatively new to Rails, and only just reaching the point where I can write code without constantly referencing others' work.
<!-- This is the "index" view, by the way. -->
<% for membership in #story.memberships %>
<% form_for membership do |f| %>
<%= f.select :permissions, [['none', 0], ['admin', 9]] %>
<% end %>
<% end %>
There are a couple ways to handle this. Certainly the observe field approach is one, but you could also use a simple javascript or a remote_function to do it:
Here is the simple JS approach:
<% remote_form_for membership do |f| %>
<%= f.select :permissions, [['none', 0], ['admin', 9]], {}, :onchange => 'this.form.submit()' %>
<% end %>
The other way would be something like this, and would eschew the form builder (and this may have a couple syntax errors):
<%= select_tag(:permissions, [['none', 0], ['admin', 9]], {:onchange => "#{remote_function(:url => permissions_path(:story_id => story,
:with => "'permission='+value")}"})
Personally I prefer the former.
Three steps!
Make your form_for a remote_form_for; add an id!
Add an observe_field after your select
Configure your observe_field to submit your form
The last bit looks something like:
<%= observe_field "id_of_select", :function => "$('form_id').submit();" %>
In Rails 3:
<%= form_for(membership, :remote => true) do |f| %>`
<%= f.select :permissions, [['none', 0], ['admin', 9]], {}, :onchange => 'this.form.submit();' %>
<% end %>
Note that it's this.form.submit(); not onsubmit. And don't forget the Javascript semicolon.
Learn how to do it without Rails using the framework of your choice. Using the Rails tags to perform AJAX can accomplish a task quickly, but can be very limiting when you need to change specific things about how the tag performs.
Read about web-standards and how to write unobtrusive javascript on these sites:
http://ajaxian.com/
http://www.alistapart.com/
You'll be able to create more flexible, amazing UIs by learning how to perform AJAX without Rails.
I am using Rails 5 & I was also facing the similar situation but in my case the answer given by #scott was not submitting the form using AJAX as expected though I added the remote: true option to the form (I don't have submit button in the form).
If somebody is also facing the similar problem try to change the JS code like this-
<% form_for membership remote: true, id: '#member_form' do |f| %>
<%= f.select :permissions, [['none', 0], ['admin', 9]], onchange: '$("#member_form").trigger("submit");' %>
<% end %>
Hope this helps..

Resources