new.html
est un formulaire d'inscription, qui crée un compte de tuteur. Il implique la gestion des erreurs. _error_messages.html.erb
est le fichier qui gère l'erreur, comme devrait remplir tous les champs de texte. par exemple, montrant: `Erreur de modèle manquant dans les rails
The form contains 3 errors.
Name can't be blank
Password confirmation can't be blank
Password confirmation doesn't match Password
Toutefois, lorsque soumet le formulaire sans entrée en new.html
, il montre l'erreur
manquant tuteurs modèle/registre, demande/inscription avec {: locale = > [: fr],: formats => [: html],: variants => [],: gestionnaires => [: erb,: builder,: raw,: ruby,: café,: jbuilder]}. Chercher dans: * "C: /RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/web-console-2.0.0/lib/action_dispatch/templates" * "D: / Sites/abc/app/vues »
new.html.erb
<% provide(:title, 'Registeration') %>
<h1>Tutor Registration</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@tutor) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirm Password" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.label :gender %>
<%= f.select(:gender, ['Male', 'Female'] , class: 'form-control')%>
<%= f.label :tutor_education_level %>
<%= f.select(:education_level, ['Bachelor', 'Master', 'Doctor'] , class: 'form-control')%>
<%= f.label :tutor_institution %>
<%= f.text_field :institution, class: 'form-control' %>
<%= f.label :tutorial_experience %>
<%= f.text_field :experience, class: 'form-control' %>
<%= f.label :tutor_preferred_district %>
<%= f.text_field :district, class: 'form-control' %>
<%= f.label :tutor_preferred_subject %>
<%= f.text_field :subject, class: 'form-control' %>
<%= f.label :tutor_required_student_level %>
<%= f.select(:student_level, ['P1-P3', 'P4-P6', 'S1-S3', 'S4-S6'] , class: 'form-control')%>
<%= f.submit "Create tutor's account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
vues/shared/_error_messages.html.erb
<% if @tutor.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@tutor.errors.count, "error") %>.
</div>
<ul>
<% @tutor.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Tuteur -Controller
class TutorsController < ApplicationController
before_action :logged_in_tutor, only: [:edit, :update]
before_action :correct_tutor, only: [:edit, :update]
def index
@tutors = Tutor.all
end
def show
@tutor = Tutor.find(params[:id])
end
def new
@tutor = Tutor.new
end
def create
@tutor = Tutor.new(tutor_params)
if @tutor.save
log_in @tutor
flash[:success] = "Congratulations! Your registration is successful!"
redirect_to @tutor
else
render 'register'
end
end
def edit
@tutor = Tutor.find(params[:id])
end
def update
if @tutor.update_attributes(tutor_params)
flash[:success] = "Profile updated successfuly!"
redirect_to @tutor
else
render 'edit'
end
end
# Handle sign-up failure, to redirect the tutor to the registeration form again
def tutor_params
params.require(:tutor).permit(:name, :email, :password, :password_confirmation, :address,:gender ,:education_level,:institution,:exprience,:district,:subject,:student_level)
end
def logged_in_tutor
unless logged_in?
store_location
flash[:danger] = "Please log in."
redirect_to login_url
end
end
def correct_tutor
@tutor = Tutor.find(params[:id])
redirect_to(root_url) unless current_tutor?(@tutor)
end
end
Affiche le contrôleur 'tutors' avec l'action' new'. –
@ defence nouveau \t @tutor = Tutor.new end' – Vito
Veuillez _post le code_ complet du contrôleur 'tutors' _à la question body_. –