Posts

Showing posts from October, 2017

Create/Update Multiple Associated Records with Checkboxes, Fix

Instead of writing a tutorial myself, I'm just going to recommend reading this post: http://millarian.com/rails/quick-tip-has_many-through-checkboxes/ Essentially creating something like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <%= form_for :interests , html : { id : "interests_form" } do | f | %> <div class="field"> <%= f . label :interests %> <br /> <%= hidden_field_tag 'user[interest_ids][]' , '' %> <% @interests . each do | interest | %> <div class="interest_option" > <%= check_box_tag 'user[interest_ids][]' , interest . id, @user . interest_ids . include?(interest . id), class : "interest_checkbox" %> <div class="interest_tag" > <%= label_tag interest . name %> </div> </div> <% end %> </div> <% end %> What I wanted to note h

Sending an Email Confirmation Link with the Right Locale (with Devise)

(This post will be short.) I have followed Railscast to configure I18n for my app, but I soon realized the link included inside the confirmation emails (sent through Devise) would direct the use to the default locale/language (i.e. English) instead of the locale that the user is using. This is my fix: In the mailer template (in devise, it would be devise/mailer/confirmation_instructions.html.erb), just pass in the user locale in the rails url helper: 1 2 3 4 5 <% user_name = ( @user . full_name . empty?) ? @user . email : @user . full_name %> <p> <%= t( '.greeting' , recipient : user_name) %> </p> <p> <%= t( '.instruction' ) %> </p> <p> <%= link_to t( '.action' ), confirmation_url( @resource , confirmation_token: @token , locale : I18n . locale) %> </p> Done.

I18n Country & City Select Fields - Reconstructing Carmen-Rails from Scratch

Image
One would think that Country/City selection fields are something so common that someone must have created a simple method to implement it. That may true to a point, but it turns out the City-State gem only provides English names, while the Carmen-Rails gem   - despite having I18n support - due to its dependence on the Carmen gem(https://github.com/carmen-ruby/carmen) - is no longer actively maintained. So, there's a few ways to get a working I18n Country/City select field working. The simplest might be to unpack the Carmen-Rails gem, change it to support Rails 5 with this fix , then install it by telling the Gemfile to install from local file. I did that initially,  but that didn't feel exactly right. So, I decided to reinvent the wheel and write the code from scratch. Now there are other gems that return country/subregions information with I18n support, such as the Countries gem and the Cities gem . However, for the purpose of making select fields, I found these gems retu