I have tweaked the bootstrap wrapper so that now I display all inline errors as a block above the simple_form field.
The form is a registration Devise form, specifically from the rails-prelaunch-signup composer app. How can I include the name of the field in the error message?
At the moment I am getting "isn't valid" or "can't be blank", however I would like something like "Email can't be blank".
Simpleform refers to rails localization if no error message is set in the model. So, if you want to add the attributes name to each error message, you could add something like this in your locale-file:
en:
errors:
messages:
blank: "%{attribute} can't be blank"
invalid: "%{attribute} isn't valid"
Where %{attribute} is the placeholder where the fields name will be inserted.
You can set the error message in the model:
validates :email, presence: { error_message: "Email can't be blank" }
To add the attribute name on every error message, you can use the full_error helper instead of the classic error helper
Directly in your form
<%= simple_form_for #user do |f| %>
<%= f.label :username %>
<%= f.input_field :username %>
<%= f.full_error :username %>
<%= f.submit 'Save' %>
<% end %>
Or in your custom wrappers
# config/initializers/simple_form.rb
SimpleForm.setup do |config|
# ...
config.wrappers :vertical_form do |b|
# ...
b.use :label
b.use :input
b.use :full_error
end
end
Related
I have the following model:
class Contact
attr_accessor :name, :emails, :message
def initialize(attrs = {})
attrs.each do |k, v|
self.send "#{k}=", v
end
end
def persisted?
false
end
end
I am calling to a contact form in my view like so:
<div class="email_form">
<%= render 'form' %>
</div>
Here is the controller:
class ShareController < ApplicationController
layout "marketing_2013"
respond_to :html, :js
def index
#contact = Contact.new
end
end
Here is the Form:
<%= form_for(#contact) do |f| %>
<%= f.label :name, "Your Name" %>
<%= f.text_field :name %>
<%= f.label :text, "Send to (separate emails with a comma)" %>
<%= f.text_field :emails %>
<%= f.label :message, "Email Text" %>
<%= f.text_area :message %>
<%= f.submit %>
<% end %>
For some reason I keep getting this error:
undefined method model_name for Contact:Class
Any reason why what I have currently wouldn't work?
Besides the correct route in your config/routes.rb, you will also need these two instructions on your model:
include ActiveModel::Conversion
extend ActiveModel::Naming
Take a look at this question: form_for without ActiveRecord, form action not updating.
For the route part of these answer, you could add this to your config/routes.rb:
resources :contacts, only: 'create'
This will generate de following route:
contacts POST /contacts(.:format) contacts#create
Then you can use this action (contacts#create) to handle the form submission.
add include ActiveModel::Model to your Contact file
your route probably doesn't go where you think it's going and therefore #contact is probably nill
run "rake routes" and check the new path.. if you are using defaults, the route is
new_contact_path.. and the erb should be in file: app/views/contacts/new.html.erb
def new
#contact = Contact.new
end
I need to set up two languages for a single form using internationalization.
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
wk:
formtastic:
labels:
detail:
dob: "Data of birtha"
name: "Youre Nama"
gender: "Gendera""
This is en.yml file
en:
formtastic:
labels:
detail:
dob: "Date of Birth"
name: "Your Name"
gender: "gender"
I have added Formtastic::SemanticFormBuilder.i18n_lookups_by_default = true in formtastic.rb initializer.
I was successful in using en.yml.
I need to switch from 'en' to 'wk' and vise-verse.
How to achieve it via drop down box?
That's not something related to formstatic, but rather to your rails code.
All you have to do in order to switch to wk is
http://guides.rubyonrails.org/i18n.html
I18n.locale = :wk
In order to let your client to choose his language for the website, probably a good place to start is on this link: http://guides.rubyonrails.org/i18n.html
If all you want is to update the form (and not the rest of the website) in different languages on a user action, like selecting the language from a select box, you can use an ajax listener on the select box, that could require something like "http://www.yourwebsite.com/:locale/form/new" which will answer with an ajax action and will replace your form with the selected language (so on :locale you will pass the value of your select box for the language).
I am new to rails and was wondering if someone could show me some light...
I have a simple form with couple of input fields and need to display field validation messages below the field name. Is there is a straightforward way to say display errors below??? or do i have to check for each field error message and create a span tag?
You can specify in your simple_form.rb initializer file with which tag your error message will be wrapped:
b.use :error, :wrap_with => { :tag => :span, :class => :error }
Also you can disable default error component on the input and print it by yourself like this:
<%= simple_form_for #user do |f| %>
<%= f.input :name, error: false %>
<%= f.error :name %>
<%= f.submit %>
<% end %>
and style your error message like you want.
If there are semantic errors in the form (mostly from external API), I'd like to add an explanatory message, like so:
<%= semantic_form_for #order, :url => checkout_purchase_url, :html => {:class => 'payment'}, :wrapper_html => { :class => "field" } do |f| %>
<% if f.has_errors? %>
<p>There were errors that prevented your order from being submitted. If you need assistance, please contact us toll-free at <strong>1-800-555-5555</strong>.</p>
<%= f.semantic_errors %>
<% end %>
<% end %>
However, has_errors? is a protected method. Is there a way that I can do this? Thanks.
If you have nested attributes you won't see any errors associated with them. To ensure you get all base errors and any nested attributes errors. Make sure your model contains:
validates_presence_of :nested_object
validates_associated :nested_object
and in your form:
f.semantic_errors *f.object.errors.keys
Not as hard as I thought. I fixed it by checking for errors on the object instead of the form:
<% if #object.errors.any? %>
<p>There were errors that prevented your order from being submitted. If you need assistance, please contact us toll-free at <strong>1-800-555-5555</strong>.</p>
<%= f.semantic_errors %>
<% end %>
Thanks for those who viewed.
For completeness, here's an alternative approach if you want to show similarly helpful messages on each field:
= f.label :title
- if f.object.errors.any?
.error = f.object.errors[:title].flatten.join(' and ')
= f.text_field :title
This gives a nicely formatted and easily-styled list of errors for each field. (You can use semantic_errors instead of object.errors if you prefer, same result.)
I have the following singular route:
scope '/seller' do
resource :seller_profile, :path => "/profile", :only => [:show, :edit, :update]
end
and the following controller:
class SellerProfilesController < ApplicationController
before_filter :validate_user_as_seller
def show
#seller_profile = current_user.seller_profile
end
def edit
#seller_profile = current_user.seller_profile
end
def update
#seller_profile = current_user.seller_profile
if #seller_profile.update_attributes(params[:seller_profile])
redirect_to(seller_profile_path, :notice => 'Profile was successfully updated.')
else
render :action => "edit"
end
end
end
I use a singular route given that the user must be authenticated before gaining access to the controller and therefore I can get the seller_profile from the user logged in.
This works like a charm, with only one problem. When I edit the seller_profile and validation error happen, the form is edited again and the errors are displayed correctly. The problem is that rails appends to the url the id of the edited record. For instance,
when I first edit the record, the url is:
http://0.0.0.0:3000/seller/profile/edit
but if the form is submitted with validation errors, the form itself is redisplayed under
http://0.0.0.0:3000/seller/profile.2
where 2 is the ID of the record being edited.
The form is the following:
<%= simple_form_for #seller_profile do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>
Everything, as said, works great but I would totally mask the ID in the url. What should I do?
I have not really worked too much with simple_form_for. But it looks like it is guessing your url always as if they were not single resources. You can provide a custom one:
<%= simple_form_for #seller_profile, :url => seller_profile_path do |f| %>
<%= f.input :name %>
<%= f.input :description %>
<%= f.submit %>
<% end %>