How to add internationalization in a formtastic page - ruby-on-rails-3

I tried to use default internationalization of formtastic and failed. I tried it as given in
https://github.com/justinfrench/formtastic/wiki/6.2-Customize:-Internationalization
So now i want to introduce internationalization using i18ln gem of rails in formtastic.
How can i do it?
This is the proposed form page
<%= semantic_form_for #detail do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :dob %>
<%= f.input :gender, :as => :radio, :label => "Gender", :collection => [["Male", 'male'], ["Female", 'female']] %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
This is wk.yml file
en:
formtastic:
dob: Date of Birth
name: Your Name
Gender: gender
How to integrate both together?

First off, have you added Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true in formtastic.rb initializer?
Try with:
wk:
formtastic:
labels:
detail:
dob: "Date of Birth"
name: "Your Name"
gender: "gender"

Related

a new page, nested form using devise's registration path

I'm trying to build a nested form in a custom page using devise.
The error that comes out is: undefined method `build_profile' for nil:NilClass
<%= form_for("user", :url => user_registration_path) do |f| %>
<%= f.email_field :email, :autofocus => true, :placeholder => 'E-mail Address' %>
<%= f.password_field :password, :placeholder => 'Password' %>
<%= f.password_field :password_confirmation, :placeholder => 'Password Confirmation' %>
<% #user.build_profile %>
<%= f.fields_for :profile do |profile_form| %>
<%= profile_form.text_field :name, :placeholder => 'Name' %>
<%= profile_form.text_field :address, :placeholder => 'Address' %>
<%= profile_form.phone_field :phone, :placeholder => 'Phone (example: 0193284647)' %>
<% end %>
<p><%= f.submit "Sign up", :class=>'btn btn-primary' %> </p>
<% end %>
Edit (Extra information):
should i add #user = #user.build_profile by creating a users_controller.rb instead?
Would it cause problems with devise?
yes you have to do
#user = #user.build_profile
in registrations_controller.rb . And,mention the customize controller in routes.rb in devise method.
You should so something like
class Registrations < Devise::RegistrationsController
def new
#user = User.new
#user = #user.build_profile
super
end
end

How to disable certain check_boxes when using Formtastic's :as => :check_boxes

<%= semantic_form_for [:admin, #admin] do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :email, :as => :email %>
<%= f.input :password, :as => :password %>
<%= f.input :password_confirmation, :as => :password %>
<%= f.input :admin_roles, :as => :check_boxes, :required => true, :disabled => [1] %>
<% end %>
<%= f.actions %>
<% end %>
-
<%= f.input :admin_roles, :as => :check_boxes, :required => true, :disabled => [1] %>
Passing an array to the above code disables the checkbox with id=1, which is nice. But I want to check the model of each checkbox, too see if it has a specific value, so I can disable it or not.
do_not_disable_checkbox if admin_role.do_not_show_me_boolean_field
Do I need to iterate each of my :admin_roles and output a checkbox for each? Or can I do it in one line like above? I'm new to rails and ruby, and I can't wrap my head around it and hoping for some help to put me in the right direction.

Rails3 acts_as_taggable with a form

I've just started working with the acts_as_taggable gem. Really liking it so far, but I am a bit unclear about how to use this gem with a form.
class Photo < ActiveRecord::Base
acts_as_taggable_on :tags
end
In my form for Photos I am trying to implement a series of checkboxes for the user to assign tags to their photo:
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
When viewing the form I get this error:
NoMethodError in Photos#edit
...line #19 raised:
undefined method `merge' for "landscape":String
Extracted source (around line #19):
18: <div class="float_tag">
19: <%= f.check_box :tag_list, "landscape" %>
Any thoughts as to how I should create my form?
I'm assuming your <form> looks something like this:
<%= form_for(#photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, "landscape" %>
<%= f.check_box :tag_list, "people" %>
<% end %>
You should change up your f.checkbox lines a bit:
<%= form_for(#photo) do |f| %>
<%= f.label :tag_list %>
<%= f.check_box :tag_list, { :multiple => true }, 'landscape', nil %>
<%= f.check_box :tag_list, { :multiple => true }, 'people', nil %>
<% end %>
Which will post something like this when submitted (with only people selected, for example):
{ :post => { :tag_list => ['', 'people'] } }
For anyone trying to get this to work with Rails 4 and strong parameters, I also had to permit the the tag_list param as an array.
params.require(:clip).permit(
:name, :other_params, { tag_list: [] }
)

Rails is not creating hidden field for put method on an update form

I have a REST resource called Greetings.
Here is my routes file:
resources :greetings
I have a form to create or update a greeting as follows:
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form') do %>
<%= text_area :greeting, :content, :rows => 3, :placeholder => "Type your message..." %>
<% end %>
note: I am using form_tag because this form also collects user data.
This is supposed to create a hidden field with method=> put but its not so it can't find the route.
Any ideas how I can get this to submit to the update action?
Just write
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form', :method => :put) do %>
and everything should be working.
You can use form_for tag and still collect user data like this:
<%= form_for #greeting, :validate => true do |f| %>
<%= f.text_area :content, :rows => 3, :placeholder => "Type your message..." %>
<%= f.fields_for #user do |u| %>
<%= u.text_field :name %>
<% end %>
<% end

How to create Rails3 forms for embedded Mongoid models?

How to create form for new User model with embedded Phone model?
I've found solution for creating form to add Phone for existing User but how to do that at the same time i create new User?
You have to create a nested form
<%= form_for #user, :url => users_path do |f| %>
<%= f.label :name, "Name:" %> <br />
<%= f.text_field :name %>
<%= f.fields_for :phone do |p| %>
<%= p.label :number, "Phone Number" %> <br />
<%= p.text_field :number %>
<% end %>
<% end %>