0
votes

This is my html.erb file.. I am getting this error

Template is missing Missing template pages/verify, application/verify with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/spritle/web-beerboard-menu/app/views" * "/var/lib/gems/2.3.0/gems/devise-4.2.0/app/views"

  <%= form_tag({:controller=>"pages", :action=>"verify"}, method: :get) do %>
  <div class="row">
  <div class=" col-lg-4 col-xs-4 col-sm-4 " >
            <label for="q">Enter date of Birth</label>
  </div>
  </div>
  <div class="row">
  <div class=" col-lg-4 col-xs-4 col-sm-4" > 
                <input id="dob" name="dob" type="date" class="form-control" required /> </div>
   </div>
  <div class="row">
  <div class=" col-lg-4 col-xs-4 col-sm-4" > 
                 <%= submit_tag 'Go', name: nil %> </div>
 </div>
  <% end %>

this is my controller file

def verify
    currentdate = Time.now.strftime("%Y-%m-%d") 
    dateofb =  Date.parse(params[:dob]).strftime("%Y-%m-%d")
    date = (currentdate.to_date - dateofb.to_date).to_i
end
5
what line exactly is throwing the error? please show the stack trace from the controller - Joel Blum

5 Answers

2
votes

The error is not about parsing of the date. It says Template is missing.

You have to add template for your verify method or add render/redirect.

1
votes

to_date requires the date in dd/mm/yyyy or yyyy/mm/dd

currentdate = Time.now.strftime("%Y-%d-%m") 
currentdate.to_date 

But current_date is in yyyy-dd-mm format which is causing

ArgumentError: invalid date

Change current_date to

currentdate = Time.now.strftime("%Y-%m-%d") 

Also,

The second issue is you need to add pages/verify.html or pages/verify.js for verify action of PagesController

0
votes

You call Pages#verify and server needs to return some response for you. The error says that you didn't specify what to return. Maybe you want to render some page (render :action)? or redirect_to some_path? Or maybe you want to return some json with dob? then you need to finish your controller action with render json: {date_of_birth: date}

0
votes

Your current error is caused by the naming of your template file. I don't know what file you're showing with your html, but you're linking to verify, and the action you posted. If you're sending data from another page, and would like to do something in the verify-action, you'll have to do so, and either have a verify.html.erb template that'll show afterwards, or a ´redirect_toorredirect_backin yourverify`-action.

It could look like this, to send the user back to where they came from before the form-submit (And fallback to root_path (This should probably be something else, though).

def verify
    currentdate = Time.now.strftime("%Y-%m-%d") 
    dateofb =  Date.parse(params[:dob]).strftime("%Y-%m-%d")
    date = (currentdate.to_date - dateofb.to_date).to_i
    redirect_back fallback_location: root_path
end

Alternative is to create views/pages/verify.html.erb to render after the form-submit.

0
votes

In the controller, the method verify should:

  • Perform some kind of verification and

    • if OK: do something (like save data) then redirect
    • else: write down a warning message to flash[:warn] and then redirect to a views where the user can correct his/her mistake.

By default, if you don't provide an explicit redirection, Rails will try to find a page view with name verify.html.erb in /views/application or /views/pages/ (assuming your controller is named Page_controller.rb).

Never forget: When you add a new method in the controller and redirect a form to that method, you need to provide explicit redirections (fail and success) or use create a specific view and use the implicit name convention.