Rails 6 Action Text - Form Validation Errors - simple-form

I'm using Action Text for a Web CRUD I've created. The form has two main attributes:
title: Título
content: Contenido
Here's my form:
<%= simple_form_for(#announcement) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<div class="form-group">
<%= f.input :title, input_html: { class: 'form-control' }, label_html: { class: 'form-label' }, required: false %>
</div>
<div class="form-group">
<label class="form-label"> <%= Announcement.human_attribute_name :cover %></label>
<%= f.input :cover, label: false, input_html: { class: 'form-control' }, as: :file, label_html: { class: 'form-label' }, required: false %>
</div>
<div class="form-group">
<label class="form-label"><%= Announcement.human_attribute_name :content %></label>
<%= f.rich_text_area :content, label_html: { class: 'form-label' }, required: false %>
</div>
<div class="form-group">
<label class="form-label"> <%= Announcement.human_attribute_name :is_active %></label>
<div class="custom-control custom-switch mr-2">
<%= f.check_box :is_active, class: 'custom-control-input', id: 'is-active-check' %>
<label class="custom-control-label" for="is-active-check" />
</div>
</div>
</div> <!-- END FORM INPUTS -->
<div class="form-actions">
<%= f.button :button, class: 'btn btn-primary mt-3' %>
</div>
<% end %> <!-- END FORM -->
In my model, I'm validating the presence of the fields:
validates :title, :content, presence: true
Problem:
When I submit an empty form the title field shows the expected validation errors. However the content field (Action Text) does not. The empty content field is preventing the record from being saved (this is OK), but like I said is not showing the error in the form.
Please take the following image as reference:
Question:
How can I show the validation errors for the content field?

The simple_form gem added support for rich text areas in version 5.0.2.
With this version, simply write
<%= f.label :content, as: :rich_text_area %>
instead of
<%= f.rich_text_area :content %>
and simple_form will work its magic.

Related

Problem with radio button tag ruby ​on rails

I am developing a simple account registration and update form and I have recently entered a parameter that can have two possible choices, in my case one problem occurred:
Problem:
<div class="tag">
<%= f.label :ruolo %><br />
<%= f.radio_button_tag(:ruolo, "admin") %>
<%= f.label_tag(:ruolo_admin, "admin") %>
<%= f.radio_button_tag(:ruolo, "user") %>
<%= f.label_tag(:ruolo_user, "user") %>
</div>
if I leave the code like this it gives me an error like:
NoMethodError in Devise::Registrations#edit
undefined method `radio_button_tag 'for # <ActionView :: Helpers :: FormBuilder: 0x00007fd6fd02f478>
this is the form:
<div class="row">
<div class="col s12 l6 offset-l3">
<br>
<br>
<h4 class="center">Registrazione</h4>
<br>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email %>
</div>
<div class="field">
<%= f.label :ruolo %><br />
<%= f.radio_button_tag(:ruolo, "admin") %>
<%= f.label_tag(:ruolo_admin, "admin") %>
<%= f.radio_button_tag(:ruolo, "user") %>
<%= f.label_tag(:ruolo_user, "user") %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<br>
<div class="actions">
<%= f.submit "Iscriviti", class: "waves-effect waves-light btn btn-devise" %>
</div>
<br>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
Controller code:
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
#roles=Role.all
devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:ruolo])
devise_parameter_sanitizer.permit(:account_update, keys: [:name,:ruolo])
end
end
radio_button_tag is not an instance method of ActionView :: Helpers :: FormBuilder, but rather ActionView::Helpers::FormTagHelper
So instead of calling it like:
<%= f.radio_button_tag(:ruolo, "admin") %>
You simply call it like:
<%= radio_button_tag(:ruolo, "admin") %>
For more details on this method please check the docs:
https://apidock.com/rails/v6.0.0/ActionView/Helpers/FormTagHelper/radio_button_tag

Ajax form not submitting - Rails 5 [duplicate]

This question already has answers here:
Rails submitting a form through ajax and updating the view
(4 answers)
Closed 5 years ago.
I am trying to submit the following form using ajax
<%= form_tag contact_form_submission_path, remote: true do %>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<%= text_field_tag :name, nil, required: true, class: 'form-control', placeholder: "Name" %>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<%= email_field :email, nil, required: true, class: 'form-control', placeholder: "Email" %>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<%= text_area_tag :message, nil, required: true, class: 'form-control', placeholder: "message", rows: '12' %>
</div>
</div>
</div>
<%= submit_tag 'Submit', class: 'btn btn-brand btn-round pull-right btn-sm'%>
<% end %>
Routes
post "/contact_form_submission", to: "pages#contact_form_submission"
Controller
def contact_form_submission
puts 'Form submit action triggered'
end
contact_form_submission.js.erb
$("form")[0].reset();
I've used this exact setup on countless occasions, and it usually works fine. However this form will not submit. I'm not getting any terminal output at all when hitting submit, and there are no console errors.
Is there something elsewhere that is required for Ajax to work that I am overlooking?
Thanks in advance.
try to change
<%= form_tag contact_form_submission_path, remote: true do %>
with
<%= form_tag "/contact_form_submission", remote: true do %>
also check is there any filter before action contact_form_submission

Issue with Application View Koudoku and Instance Variable

In my application I have created a contact form in the footer of the page and hence it's been placed within the application layout. I have also set up Koudoku to manage my stripe payments on the site.
Via the Koudoku documentation it says since Koudoku uses the application layout all paths must be prefixed with main_app.
This has been implemented for all the links already, however the contact form is using a instance variable declared in the application controller like this.
def set_contact
#contact = Contact.new
end
and the form in the application view looks like this
<%= form_for #contact do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :comments %>
<%= f.text_area :comments, class: 'form-control' %>
</div>
<%= f.submit 'Submit', class: 'btn btn-default' %>
<% end %>
I am getting this error here
undefined method `contacts_path' for #<#<Class:0x007f8b4d6bb658>:0x007f8b5033fc88>

Post List of Values to model params

I am creating instructors page which have multiple course ids to be passed to the model parameter list.
The following is my code for whitelist params
def instructor_params
params.require(:instructor).permit(:LastName, :FirstMidName, :HireDate, {:courses_ids => []})
end
My create page is like below
<%= form_for #instructor, :html => { :class => "form-horizontal instructor" } do |f| %>
<% if #instructor.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#instructor.errors.count, "error") %> prohibited this instructor from being saved:</h2>
<ul>
<% #instructor.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :LastName, :class => 'control-label col-md-2' %>
<div class="col-md-10">
<%= f.text_field :LastName, :class => 'form-control' %>
<%= error_span(#instructor[:LastName]) %>
</div>
</div>
<div class="form-group">
<%= f.label :FirstMidName, :class => 'control-label col-md-2' %>
<div class="col-md-10">
<%= f.text_field :FirstMidName, :class => 'form-control' %>
<%= error_span(#instructor[:FirstMidName]) %>
</div>
</div>
<div class="form-group">
<%= f.label :HireDate, :class => 'control-label col-md-2' %>
<div class="col-md-10">
<%= f.text_field :HireDate, :class => 'form-control' %>
<%= error_span(#instructor[:HireDate]) %>
</div>
</div>
<div class="form-group">
<%= f.label text="Courses", :class => 'control-label col-md-2' %>
<div class="col-md-10">
<table cellpadding="5">
<tr>
<td>
<input type="checkbox" name="course_ids" value="Course1">Course1
<input type="checkbox" name="course_ids" value="Course2">Course2
<input type="checkbox" name="course_ids" value="Course3">Course3
</td>
</tr>
</table>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
instructors_path, :class => 'btn btn-default' %>
</div>
</div>
<% end %>
But if select multiple check-boxes params not passing as arrays. ?It took only the last one and showing not permitted. Please guide me. I don't have any attribute named course_ids in Instructor model.
To receive an array of values, you need to change the name of your checkboxes to be course_ids[]
In your permitted params, {:courses_ids => []} can be written simply as course_ids: [] (also notice the typo you had with the plural 'courses').

bootstrap navbar search embedded ruby

I have this code for a search bar in my nav bar.
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search an Artist">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
And I want it to correlate to this embedded ruby code
<%= form_tag(artists_path, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Artists" %>
<%= submit_tag "Search", :name => nil %>
<% end %>
I tried to just remove the div class ="form-group" and submit button and then put the ruby code within the form class, but then it changes the look of it and will only work when I'm already on my artists page, I want it to work from anywhere on my site. Any suggestions on how to integrate the ruby code into the html one?
This should work:
<%= form_tag(artists_path, :method => "get", id: "search-form", html: {class: 'form-group'}) do %>
<%= text_field_tag :search, params[:search], placeholder: "Search Artists", class: "form-control" %>
<%= submit_tag "Search", :class => 'btn btn-default',:name => nil%>
<% end %>

Resources