How to put custom alert messages for RubyonRails forms - ruby-on-rails

<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<% if alert %>
<p class="alert alert-danger"><%= alert %></p>
<% end %>
<div>
<%= f.email_field :email, autofocus: true, placeholder: 'Correo Electrónico' %>
</div>
<div class="m-t">
<%= f.password_field :password, autocomplete: "off", placeholder: 'Contraseña' %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="m-t"><%= f.check_box :remember_me %> <%= f.label :remember_me, '¿Recordar?' %></div>
<% end -%>
<div><%= f.submit "LOG IN", class: 'custom-input-2' %></div>
<% end %>
I'm trying to make the alert show a custom message instead of the INVALID EMAIL OR PASSWORD default one.
Is there a documentation about this somewhere or can someone explain to me how this is done.

Related

How can i add an FontSwesome icon in the same line?

I use my rails with devise (login system). That works. Now I will add in FA-Icon to as my label.
This is my target:
But this my current state (the hook is my test icon):
That is my code:
<div class="container">
<%= form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: "form-signin"}) do |f| %>
<h2 class="form-signin-heading">Please sign in</h2>
<%#= f.label :email, 'Email Address', class: 'sr-only' %>
<i class="fa-solid fa-check"></i>
<%= f.email_field :email, autofocus: true, class: 'form-control', placeholder: 'Email Address' %>
<%= f.label :password, 'Password', class: 'sr-only' %>
<%= f.password_field :password, autocomplete: "off", class: 'form-control', placeholder: 'Password' %>
<% if devise_mapping.rememberable? -%>
<div class="checkbox">
<label><%= f.check_box :remember_me %> Remember me</label>
</div>
<% end %>
<%= f.button "Sign in", class: 'btn btn-lg btn-primary btn-block' %>
<% end %>
<%= render "devise/shared/links" %>
</div>
Can someone give me a hint, what i am doing wrong?

Devise's edit user page doesn't want to save a text area I've added

As the title says, I've added a text area to devise's edit page, but whenever I click Update, it doesn't update it, it just stays blank all the time.
Here's my edit view:
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :current_goals %><br />
<%= f.text_area :current_goals, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
<% if #minimum_password_length %>
<br />
<em><%= #minimum_password_length %> characters minimum</em>
<% end %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
application_controller:
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:other_attributes, :current_goals) }
end
However, if I don't define a method called current_goals in my user.rb, rails will give me an error, undefined method current_goals.
user.rb
def current_goals
end
How do I make it save the information I give it?

Updating user information outside devise pages

I want to add the option for the user to edit his email/password/image... from the /settings page not from the /users/edit page. So I copied the code to the settings page
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: 'form-control'%>
</div>
<div class="form-group">
<%= f.label :cover %>
<%= f.file_field :cover, class: 'form-control'%>
</div>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
but it returned this
undefined local variable or method `resource' for #<#<Class:0x007f60245ab9b0>:0x007f6026f58358>
What should I add to my controller?
When ever you want to perform any custom actions, first create an action in a controller, then a route to it and then the view file.
resource object is only available in devise routes.
So, first add a method named settings in any controller eg: say userscontroller.
class UsersController < ApplicationController
def settings
#user = current_user
end
end
Then in routes.rb, add a route which points to this action with a route named '/settings'
get '/settings' => 'users#settings', as: :settings
Now, in app/views/users/settings.html.erb
<%= form_for(#user, url: registration_path(#user), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="form-group">
<%= f.label :image %>
<%= f.file_field :image, class: 'form-control'%>
</div>
<div class="form-group">
<%= f.label :cover %>
<%= f.file_field :cover, class: 'form-control'%>
</div>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
<%= link_to "Back", :back %>
So, in the show page of user add a link to settings page,
<%= link_to 'Edit', settings_path(user) %>

appropriate usage of hidden_fields

I am sending one parameter ("designer" or "developer") to register form prepared by Devise and I want to add this parameter to devise model.
My question is if assigning parameter to hidden_field is appropriate solution.
From this view I'm redirecting to user registration form
<%= link_to "Register as Owner", new_user_registration_path(:role => 'owner') %>
<%= link_to "Register as Employee", new_user_registration_path(:role => 'employee' ) %>
User registration form
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :role, :value => params[:type]%>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</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>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
As long as the param doesn't contain sensitive information (passwords etc) as you say it doesn't, there isn't a problem with your implementation.

Form error because of rendering form in modal

Rails project with materialize framework, what I am trying to do is rendering a form in a modal.
Therefore I have in the application.html.erb:
<!-- Modal Structure -->
<div id="signupmodal" class="modal">
<div class="modal-content">
<%= render template: "users/new" %>
</div>
</div>
The rendered template looks like this:
<%= form_for(#user) do |f| %>
<%= render 'shared/error_messages' %>
<div class="row">
<div class="row col s6">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>
</div>
<%= f.submit 'Create my account', class: "btn waves-effect waves-light orange darker-1" %>
<% end %>
But because the rendered template has its own controller, I get the
First argument in form cannot contain nil or be empty
error, because it doesn't know the #user in the application controller. What is the best way to avoid this error?

Resources