I'm trying to fill in an ActionText rich_text field form input, but can't figure out how to select it. I'm using Rails 6 and ActionText.
With
class Activity
has_rich_text :description
end
and _form.rb
= f.label :description
= f.rich_text_area :description, class: 'form-control'
Test using:
fill_in "Description", with: "Some description.."
I'll get the error
Capybara::ElementNotFound: Unable to find field "Description" that is not disabled
I suspect the problem is with how the Trix editor dynamically fills in this field as you type. I'm just not sure how to do the input, replicating how the user would be entering text.
You can do so by find the trix editor and entering your details manually:
find(".trix-content").set("New value")
Since Rails 6.1, you can use the fill_in_rich_text_area system test helper. Added in b2b6341374.
Related
I have a form within my app that I build using simple_form. What I am trying to do is edit my error messages to something a little different than the default. I am working on my simple_form.en.yml file and I think that may be where my problems are happening.
With what I have, i'm not sure I understand what exactly goes in the settings file, and I am hoping someone can go over what I have and advise me on where to go.
My model is like so (this is my entire model)
class FormSubmission < ActiveRecord::Base
after_create :email_sales
validates :first_name, :last_name, :organization, :email, :phone, :recognition, :inquiry, presence: true
private
def email_sales
FormSubmissionMailer.update_sales(self).deliver_now
end
end
Here is one of the areas of my view
= simple_form_for #form_submission do |f|
.fieldSet.span8
.field.reco
= f.input :first_name, input_html: { class: "formStyling" }, label: "First name", required: false
Finally, in my simple_form.en.yml file I have this here
en:
activerecord:
errors:
models:
formsubmission:
attributes:
email:
blank: "cannot be empty"
Simple_form does nothing special for validation error messages I18n and leaves all work to the default Rails I18n processing. The simple_form.en.yml localization file only deals with the various options to display the form and its elements (labels, hints, etc., see the docs) and has nothing to do with error messages.
So, if you need to set the error messages localization, have a look at the official Rails guide on I18n instead. Actually, I think that your simple_form.en.yml example might work, if you moved the error message localizations to the default Rails locale file for English: config/locales/en.yml.
First time I try to customize rails_admin and by default I am getting text field which I want to convert in textarea. In model I have given datatype as string So is that possible to display textarea?
config/initializer/rails_admin.rb
config.model Product do
list do
exclude_fields :id, :created_at, :updated_at
end
create do
......
configure :description do
partial 'my_partial_file' # to override field I have created partial file
end
.....
end
end
views/rails_admin/main/_my_partial_file.html.haml
= form.send field.view_helper, field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
I tried html_attributes rows: 50, cols: 60 also tried to apply custom_css but doesn't help. Please guide me where I do mistake? And if possible please make me understand syntax of this _my_partial_file
Edit:
If I do something like this then I can get textarea
field :description, :text do # use second parameter to set field type
required true
#partial 'my_partial_file'
end
But if I render partial then again text_field shown. :( I want text_area + partial file should also rendered as it contain other code to process.
I have solved this issue by my own. I thought to delete this question but then realized if in future anyone faced same issue then my solution can be helped.
What I had changed in _my_partial_file is:
= form.text_area field.method_name, field.html_attributes.reverse_merge({ value: field.form_value, checked: field.form_value.in?([true, '1']), class: 'form-control', required: field.required})
........ # other piece of code
and in config/initializer/rails_admin.rb I have keep the code as it is:
create do
......
configure :description do
partial 'my_partial_file'
end
.....
end
I got the reference from here: https://www.omniref.com/ruby/gems/obitum-rails_admin/0.0.5/files/app/views/rails_admin/main/_form_text.html.haml#line=5 (Wayback link)
and
http://ruby-doc.org/gems/docs/r/rails_admin_settings-0.8.0/app/views/rails_admin/main/_setting_value_html_haml.html
(Note: link dead. This may be the same file from that older version: https://github.com/rails-admin/rails_admin_settings/blob/v0.8.0/app/views/rails_admin/main/_setting_value.html.haml )
So, I'm basically trying to show Devise's confirm_within time in the error message that comes when clicking the confirmation email link after the required time.
I am using YAML translation files.
The interpolation variable is called devise_confirm_within
My YAML is like this:
en:
activerecord:
errors:
models:
user:
attributes:
email:
confirmation_period_expired: "some text %{devise_confirm_within}"
Normally, I would find the appropriate view, and then pass the interpolation variable as a parameter to the translate or t method. Like this:
<p><%= t("devise.mailer.confirmation_instructions.please_click_the_below_link_to_confirm_your_new_email", :devise_confirm_within => distance_of_time_in_words(0, User.confirm_within, :locale => I18n.locale)) %></p>
However, there is no view that has the confirmation_period_expired translation key, so I can't do it in this same manner.
I have a view with:
<div class="form-inputs">
<%= f.input :email, :required => true %>
</div>
And a user model with:
validates :email, uniqueness: true
I want to keep all the translation text in the YAML file, so adding :message to the validates field won't do.
So how can I get the devise_confirm_within time into that confirmation_period_expired error message?
PS: I am using the simple_form gem for forms, and also the dotiw gem for overriding the default distance_of_time_in_words method (this doesn't influence this problem I'm having).
The problem was circumvented when I added this to devise.en.yml and similar to the other devise..yml files:
en:
errors:
messages:
confirmation_period_expired: "some text %{period}"
I however wasn't able to pass a translation interpolation variable to a failed form validation (i.e. add a variable to a devise message), of which I am still curious.
While I was writing cucumber test code,
I get:
Unable to find field "username" (Capybara::ElementNotFound)
But I have the following on the page itself.
<%= f.text_field :username, :placeholder => "Username" %>
I've checked that it lands on the correct page using
save_and_open_page
fill_in "username", :with => "TESTUSER"
isn't the tag :username? What am I supposed to write instead?
Capybara will match fields based on their id, name or label text. See here for more details.
Since you are using the default text_field helper, id and name will default to include the model (e.g. user_username for the id, or user[username] for the name). You can change these defaults by simply using id: <id> or name: <name> on your text_field call but you might run into other problems later - so probably best to stay with the defaults.
Change your capybara test to fill_in "user_username" or fill_in "user[username]" to make it match. Alternatively, if you have a label_for on the field, you can match on the text of the label which can make your tests more readable.
I'm using the simple_form gem. I want to customize the error message displayed when a user fails validations. How can I accomplish this?
You can declare the content of the
error message in your model:
validates_length_of :name, :minimum => 5, :message => "blah blah blah"
You can set id or class for your
error tag:
<%= f.input :name, :error_html => { :id => "name_error"} %>
Then you can use CSS for the styling.
And you can use
<%= f.error :name, :id => "name_error" %>
and you'll get
<span class="error" id="name_error">is too short (minimum is 5 characters)</span>
I dont know if it is any different for simple_form gem.
For content of error messages to be changed, you can use the :message attribute in the model.
class User < ActiveRecord::Base
validates :email, {:presence => true, :message => "is not filled up."}
end
Now the validation message will be Email is not filled up. If you want the field name also to be changed(Email to E-mail address something like that ), the approach now is to define it in locales.rb file like this
# config/locales/en.yml
en:
activerecord:
attributes:
user:
email: "E-mail address"
See link for details on locales. Another approach is to define in the model, humanized attributes like this:
class User < ActiveRecord::Base
validates :email, {:presence => true, :message => "is not filled up."}
HUMANIZED_ATTRIBUTES = {
:email => "E-mail address",
...(other fields and their humanized names)
...
}
def self.human_attribute_name(attr, options={})
HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end
end
For customizing style of validation message we will have to edit the style for
#errorExplanation and .fieldWithErrors,in the scaffold.css stylesheet.
You can easily change the default error message comes in the translation file, which is found in config/locales/simple_form.en.yml.
In the specific initializer, config/initializers/simple_form.rb you can overrule the default options how the html is generated.
Hope this helps.
For completeness, I would like to add that formtastic is an easier choice to start with, because it has a default layout. I like simple_form a lot, but it does not offer any formatting out of the box, but that is their intention. With Formtastic it is very hard (impossible) to change the generated html, and with simple_form can you can completely mold the generated html to your liking. This is especially useful if you have a designer, and the forms you generate have to generate the same html. So if you are getting started, formtastic will give you nicer-looking results quicker. Also note that it is quite easy to switch, because the syntax is almost identical.
There is another solution explained here that wasn't mentioned in the answers. You can directly override the error messages from the views, in the form itself. For example:
<%= f.input :last_name,
placeholder: 'last_name',
error: 'This is a custom error message',
required: true,
class: 'form-field',
autofocus: true,
input_html: { autocomplete: "last_name" } %>
It is however not advised as it is not DRY, you would need to override it in every field.