Reputation: 45
I have built a sign up page in ruby on rails. I want to validate this sign up form. For example - if user left any section blank, the form will show an error message. It will also check whether the given email is unique. How can I do this by not changing my form?
#This my logins_controller.rb file
class LoginsController < ApplicationController
skip_before_action :verify_authenticity_token
def index
@subscriber=Subscriber.new()
end
def sign_up
subscriberNew = Subscriber.new
subscriberNew.name = params[:name]
subscriberNew.cus_user_name = params[:user_name]
subscriberNew.cus_password = params[:password]
subscriberNew.cus_email = params[:email]
result = subscriberNew.save
respond_to do |format|
msg = { :status => "ok", :message => "Success!" }
format.json { render :json => msg }
end
end
def validate_signup
#what should i write here?
end
end
and this is my sign up form
<div class="container">
<div class="shadow-lg p-3 mb-5 bg-white rounded view-card">
<h4 class="card-title">
<a href="/product_types">
<i class="material-icons">
arrow_back_ios
</i></a></h4>
<form id="signup_form" method="post" action="/sign-up">
<p class="h4 mb-4">Register to Binimoy</p>
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control mb-4" placeholder="Name">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control mb-4" placeholder="Email">
<label for="phone">Phone</label>
<input type="text" name="phone" id="phone" class="form-control mb-4" placeholder="Phone">
<label for="name">Password</label>
<input type="password" name="password" id="password" class="form-control mb-4" placeholder="Password">
<div class="form-row mb-4">
<div class="col">
<button class="btn btn-default my-4 btn-block" type="reset">Reset</button>
</div>
<div class="col">
<button type="button" class="btn btn-info my-4 btn-block" id="submitAnchor">Submit</button>
</div>
</div>
</form>
</div>
Upvotes: 0
Views: 1549
Reputation: 8646
Rails is an framework with an strong opinion how to do things. You are doing things not the Rails way and this will make it more complicated.
If you really do not want to change your form (which is strongly advice against)
Add validations, check the return value of save
and send back some error message
In your model:
validates :email, presence: true, uniqueness: true
In your controller:
if subscriber.save
render json: {status: "ok", message: "success"}
else
render json: {status: "nok", message: "something went wrong"}
end
Probably you should get rid of the status
property and use the HTTP status code for this (sending back 201 (created) and 422 (unprocessble entity) responses instead)
Some other recommendatations to make your code more readable:
Use Ruby naming conventions:
subscriberNew
should be
subscriber_new
since Ruby uses camel_case
but actually there is no need for the new
suffix, so subscriber
is enough
cus_username
, ...) and don't prefix when there is no needUpvotes: 1
Reputation: 717
First, use Rails helpers in your views and write your form as documented here.
<%= form_with url: sign_up_path do |form| %>
<%= form.label :email %>
<%= form.text_field :email %>
...
<%= form.submit %>
<% end %>
Second, use validations in your Subscriber model as documented here.
validates :email, presence: true, uniqueness: true
...
Hope it helps ;)
Upvotes: 1