Displaying countries using country_select gem in Rails 4 - ruby-on-rails

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.

Related

Form Required Doesn't Work

I have tested three ways of doing a field required, first with no gem, just the usual form_for and it did work well, but I need some good gem for making easier adding fields to insert associations, then I installed the Simple Form. Here is the code I am using:
<%= simple_form_for #post do |p| %>
<%= p.text_field :title, :required => true %> <br />
<%= p.input :content, required: true%> <br />
<%= p.input :category_id, input_html: { required: true }%>
<%= p.submit %>
<% end %>
See how I used all the three ways of getting required to true and the usual way of creating a text field of the form_for so I can see if I find a solution. No success. Even after making config.browser_validations = true in config/initializers/simple_form.rb. Why is it working for form_for but not when I am using gems? I also tried Formtastic and had the same issue.
If you place required: true in the input you should see the field has the "required" class and required="required" attribute.

Trouble dynamically adding fields with rails

I'm dynamically generating nested form fields using the cocoon gem as follows:
<%= simple_form_for #incorporation do |f| %>
<%= f.simple_fields_for :company do |company| %>
<%= link_to_add_association 'Add ID', company, :persons, class: "btn btn-default add-button" %>
<%= company.simple_fields_for :persons do |person|%>
<%= render 'person_fields', f: person %>
<% end =%>
<% end =%>
<% end =%>
And _person_fields is (currently) as follows:
<div>
<div class="col-md-6"><%= f.input :fname, input_html: {class: 'form-input form-control'}, label: "First Name" %></div>
</div>
The link_to_add_association should, through the cocoon gem and javascript, add another row of _person_fields to the form
The problem is that the button in fact adds nothing. Rather, it seems to just bring me to the top of the page. I know that javascript is installed (and working) via therubyracer gem. I know javascript is working because I've got bootstrap running on the site.
I know cocoon works and I've used it on a few apps. Does anyone see something that I might be leaving out?
Turns out I needed to RFTM I was missing the line:
//= require cocoon
in my application.js
I figure I'll keep this question up in case anyone else needs the reminder as well :-)

How to use one partial in different contexts

i am new to rails and try to do the following:
I would like, because i use Bootstrap, to have a partial for a input field, with it's label and a little icon i called symbol in this case.
Here is my view:
<%= form_for(#user, :class => "form-horizontal" ) do |f| %>
<fieldset>
<%= render 'shared/text_field', function: f, tag: :name, symbol: '<i class="icon-user"></i>' %>
<%= render 'shared/text_field', function: f, tag: :email, symbol: "#" %>
<%= render 'shared/password_field', function: f, tag: :password, symbol: '<i class="icon-log"></i>' %>
<%= render 'shared/password_field', function: f, tag: :password_confirmation, alt: "Passwort wiederholen", symbol: '<i class="icon-log"></i>' %>
<!-- SUBMIT -->
<%= f.submit "Anmeldung", :class => "btn btn-primary" %>
</fieldset>
<% end %>
Here a subpartial for normal input fields:
<%= render 'shared/bootstrap/input_field' %>
<% content_for :label do %>
<%= function.label tag, :class => "control-label", :for => "prependedInput" %>
<% end %>
<%content_for :symbol do %>
<%= raw(symbol) %>
<% end %>
<% content_for :field do %>
<%= function.text_field tag, :class => "input-xlarge", :id => "prependedInput", :size => "6" %>
<% end %>
(there is also a subpartial for password fields, basicly exchanging function.text_field with function.input_field)
And here is the input_field which is rendered:
<div class="control-group">
<%= yield :label %>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><%= yield :symbol %> </span>
<%= yield :field %>
</div>
</div>
</div>
So my question is: how can i solve this Problem in a nice and easy way (this things happend while refactoring and it got even worse then better) and how can i make it work, because by now something like this happens: http://pastebin.com/bNsgT9AR so yield puts with each content_for the content and the content before into the place (except the last one)
It would be great to hear nice solutions from you, there has to be a so much nicer way as almost always in Rails.
Greetings form Germany ;)
As the author of The Rails View, I agree with Ben Taitelbaum's post on this and say that Formtastic is totally a great option for this kind of complexity.
With regards to the code you posted, as developers we want to avoid that kind of view writing across the board, as it ends up an unmanageable mess. There's too many files involved in editing a form, and at some point, some other dev will come in and overwrite our shared partial, not knowing where else it was used, to change the way a different form behaves. Any benefit of code reuse that we can get out of this kind of abstraction is completely obliterated by the potential for it to go sour very quickly with mutliple developers.
While Formtastic is great, I've also been using SimpleForm a lot in my recent apps, especially when I don't need the full power of Formtastic.
SimpleForm will handle your label, and it also supports i18n as well. With this it would be:
<%= simple_form_for #user do |f| %>
<%= f.input :name, :input_html => { :class => 'user-icon' } %>
<%= f.input :email %>
<%= f.input :password, :as => :password, :input_html => { :class => 'log-icon' } %>
<%= f.input :password_confirmation, :as => :password, :input_html => { :class => 'log-icon' } %>
<%= f.button :submit %>
<% end %>
Then in your I18n file, you can have:
en:
simple_form:
labels:
user:
username: 'User name'
password: 'Password'
password_confirmation: 'Password confirmation'
de:
simple_form:
labels:
user:
username: 'Benutzername'
password: 'Passwort'
password_confirmation: 'Passwort wiederholen'
And more as you need it. You can define your hints and placeholders in the i18n file as well.
Also, think about adding the icon as a CSS pseudo class :after, and do so for each class that you need to add in the code)
input.user-icon:after {
// image as background, positioned properly, etc etc
}
input.log-icon:after {
// image as background, positioned properly, etc etc
}
And that way, you can remove this kind of stuff from your ERB/HTML and put it where it belongs: In the CSS (presentation layer).
There's a great section on forms in The Rails View, where they recommend using FormBuilders for this type of thing (they also discuss formtastic, which is worth checking out).
The biggest lesson I got out of this book (and I can't recommend it enough!) is that it's important to keep views as simple as possible, since they tend to be the least fun part of a rails app to debug, especially as the app grows large. So from this perspective, I would want the view to be nice and simple, something like:
<%= semantic_form_for(#user) do |f| %>
<%= f.inputs do %>
<%= f.input :name, class: 'icon-user' %>
<%= f.input :email %>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<% end %>
<%= f.buttons %>
<% end %>
Start with a nice readable view as the goal, and add the necessary helpers and formbuilders as needed for custom field types.
I'd recommend using I18n for setting the submit button text to be "Anmeldung" instead of hardcoding that as well.
I haven't used it, but you might want to check out formtastic-bootstrap for getting the integration you want..
I feel that's a bit too much generalization overhead for a simple input field.
Have you considered simply creating one shared input field partial and just passing in the field name (you named it tag) and the type of field (text_field or password_field) as parameters? I can see that your approach might be more general, but if you just need those two options for now, keep it as simple as possible.
Addition in reply to comment:
You can simply pass parameters to your partial like so:
<%= render 'shared/error_messages', object: f.object %>
And then use the the variables in your partial. in your case you could
<%= render 'shared/bootstrap/input_field', function: f, tag: :name, input_field_type: :text_field %>
and then in _input_field.html.erb
<div class="control-group">
<%= function.label tag, :class => "control-label", :for => "prependedInput" %>
...

Using Formtastic for checkboxes for a habtm association in Rails3

I followed the directions in Railscast #17 HABTM Checkboxes (revised) to get this code for adding services to a project using a has_and_belongs_to_many association:
<% Service.all.each do |service| %>
<%= hidden_field_tag "project[service_ids][]", nil %>
<%= check_box_tag "project[service_ids][]", service.id, #project.service_ids.include?(service.id), id: dom_id(service) %>
<%= label_tag dom_id(service), service.name %><br />
<% end %>
This works correctly, but I'd like to use Formtastic to generate the code in order to keep the formatting consistent with the rest of the page. The video mentions that Formtastic can do this easily, but I can't figure out the code for the life of me.
My guess was to do something like this:
<%= semantic_form_for :services do |f| %>
<%= f.input :name, :as => :check_boxes, :collection => Service.find(:all) %>
<% end %>
and that generates the list of services, but checking the boxes does not do anything. I know that last bit of code need to be linked somehow to the projects_services association, but I don't know how to do it.
Okay, I was trying to make this harder than it is. This is all I had to do:
<%= f.input :services, :as => :check_boxes %>

Rails i18n and yml structure for form labels

According to the ActionView documentation. Quote:
The text of label will default to the attribute name unless a translation is found in the current I18n locale (through views.labels.<modelname>.<attribute>) or you specify it explicitly.
I have a "user" model and a registration form. Here's a snippet of the relevant part:
<% form_for(#user) do |f| %>
...
<p>
<%= f.label :username %>
<%= f.text_field :username, :class => 'full_width' %>
</p>
...
<% end %>
Dots hide unimportant code.
As I understand the documentation, if I provide a translation in my locale file, in this case :dk, my dk.yml looking like so:
dk:
views:
labels:
user:
username:
"blahblah"
Rails should translate the label text and insert "blahblah" instead of "Username".
This is not happening, so I must have missed something. Any help appreciated.
In Rails 3.1 that is a little bit changed.
<% form_for #post do |f| %>
<%= f.label :title %>
<%= f.text_field :title %>
<%= f.submit %>
<% end %>
en:
helpers:
label:
post:
title: 'Customized title'
I think I found another solution here.
My app was version 2.3.5. I've now changed it to 2.3.8 and <%= f.label :username %> now uses the translation in:
dk:
activerecord:
attributes:
user:
username:
I found the hint in this ticket:
https://rails.lighthouseapp.com/projects/8994/tickets/745-form-label-should-use-i18n
That's because the label method you are calling is not the one from ActionView::Helpers::FormHelper but is in fact the label_tag method from ActionView::Helpers::FormTagHelper. The form_for method is rewriting the code in the given block by adding _tag to the used form helpers. So you're not looking at the documentation for the right method!
I've not yet used that method, as sometimes the label for a field can be different from multiple forms using the same model, so I've written my own helper.

Resources