Changing simple_form submit button i18n text - ruby-on-rails

This is my code:
<%= simple_form_for [:backend, #department] do |f| %>
<%= f.input :parentid, input_html: { class: 'form-control' } %>
<%= f.input :name, input_html: { class: 'form-control' } %>
<%= f.button :submit %>
<% end %>
This is my form and I want to change the submit text, now the text is create department, so I tried:
zh-CN:
simple_form:
helpers:
submit:
department:
create: "新建部门"
update: "保存编辑"
but nothing changed. How can I change it?

I add a gem i18n-debug, then I restart server and flash the page. Look the terminal log , I know what is the problem。
enter image description here
so, after zh-CN is helpers, not simple_form

Bruce Wayne give excellent answer.
But I'v got error in i18n-debug:
private method `prepend' called for I18n::Backend::Simple:Class
(NoMethodError)
on debug.rb
I changed
Backend::Simple.prepend(Debug::Hook)
on
Backend::Simple.send :prepend, Debug::Hook
and it works fine!
Or you can use patched gem in your Gemfile:
gem 'i18n-debug', github: 'robotex82/i18n-debug', branch: 'robotex82-patch-1'
from https://github.com/robotex82/i18n-debug/tree/robotex82-patch-1

i18n_debug_page provides a UI for i18n debug info, it looks like as below:

Related

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.

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 carmen country_select helper without a form object

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' %>

Rails: Using simple_form and integrating Twitter Bootstrap

I'm trying to build a rails app and simple_form looks to be a really useful gem. Problem is that I am using twitter bootstrap css to do the styling and the simple_form doesn't allow you to specify the layout of the html.
Can anyone tell me how I can conform the simple_form html into the format bootstrap css wants?
Note: This answer only applies to SimpleForm < 2.0
Start with this in config/initializers/simple_form.rb:
SimpleForm.form_class = nil
SimpleForm.wrapper_class = 'clearfix'
SimpleForm.wrapper_error_class = 'error'
SimpleForm.error_class = 'help-inline'
Then there's space missing between the label element and the input, which you can add with this:
label {
margin-right: 20px;
}
There's probably more, but it's a start.
Simple form 2.0 is bootstrap-aware:
rails generate simple_form:install --bootstrap
I recently had the same problem and tried out the combination of bootstrap-sass and formtastic-bootstrap.
It works with the exactly same code as the code for formtastic and shows even error messages as expected.
bootstrap-sass also works with Rails 3.1 Asset Pipeline and Rails 3.0. formtastic-bootstrap is tested with RSpec so I think this is a nice way to go!
I wrote a gem to do exactly this. It's not simple_form, but it should work fine side-by-side: twitter_bootstrap_form_for.
Here's the full config (config/initializers/simple_form.rb):
SimpleForm.setup do |config|
config.hint_class = 'help-block'
config.error_class = 'help-inline'
config.wrapper_class = 'clearfix'
config.wrapper_error_class = 'error'
config.label_text = lambda { |label, required| "#{label} #{required}" }
config.form_class = nil
end
simple_form allows for a css class (Passing in :html => {:class => nil} will result in only a "simple_form" class.).
n.b. This was added on 7/25/2011 so many existing downloads and documentation will not have it. You could also wrap it in a div that specifies a style.
You can also always style an individual element with code such as
<%= f.input :username, :label_html => { :class => 'my_class' } %>
I found this, seems working fine https://github.com/rafaelfranca/simple_form-bootstrap don't known about tight integration
If you use SimpleForm 1.5 the authors of the gem provide you with the required configuration instructions here: https://github.com/plataformatec/simple_form/wiki/Twitter-Bootstrap-integration
In this railscast: http://railscasts.com/episodes/329-more-on-twitter-bootstrap, you can see how to customize simple_form with twitter bootstrap (skip to 3:05).
terminal :
rails g simple_form:install --bootstrap
model/_form.html.erb :
<%= simple_form_for #product, :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
<legend><%= controller.action_name.capitalize %> Product</legend>
<%= f.input :name %>
<%= f.input :price %>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to 'Cancel', products_path, :class => 'btn' %>
</div>
</fieldset>
<% end %>

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