1

I just installed devise and below is the specific error that I'm getting:

enter image description here

Which is weird because I have <%= csrf_meta_tags %> inside the head tags of my application.html.erb file & <%= form_authenticity_token %> in all my forms.

Below are all the files that are associated with the devise gem:

These are the layout files:

<h2>Resend confirmation instructions</h2>

<%= simple_form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
  <%= csrf_meta_tags %>
  <%= form_authenticity_token %>
  <%= f.error_notification %>
  <%= f.full_error :confirmation_token %>

  <div class="form-inputs">
    <%= f.input :email,
                required: true,
                autofocus: true,
                value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email),
                input_html: { autocomplete: "email" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Resend confirmation instructions" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

<h2>Change your password</h2>

<%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
  <%= csrf_meta_tags %>
  <%= form_authenticity_token %>
  <%= f.error_notification %>
  <%= f.input :reset_password_token, as: :hidden %>
  <%= f.full_error :reset_password_token %>

  <div class="form-inputs">
    <%= f.input :password,
                label: "New password",
    https://stackoverflow.com/questions/ask#            required: true,
                autofocus: true,
                hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
                input_html: { autocomplete: "new-password" } %>
    <%= f.input :password_confirmation,
                label: "Confirm your new password",
                required: true,
                input_html: { autocomplete: "new-password" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Change my password" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

<h2>Forgot your password?</h2>

<%= simple_form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
  <%= csrf_meta_tags %>
  <%= form_authenticity_token %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :email,
                required: true,
                autofocus: true,
                input_html: { autocomplete: "email" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Send me reset password instructions" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= csrf_meta_tags %>
  <%= form_authenticity_token %>
  <%= f.error_notification %>
  <div class="form-inputs">
    <%= f.input :email, required: true, autofocus: true %>

    <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
      <p>Currently waiting confirmation for: <%= resource.unconfirmed_email %></p>
    <% end %>

    <%= f.input :password,
                hint: "leave it blank if you don't want to change it",
                required: false,
                input_html: { autocomplete: "new-password" } %>
    <%= f.input :password_confirmation,
                required: false,
                input_html: { autocomplete: "new-password" } %>
    <%= f.input :current_password,
                hint: "we need your current password to confirm your changes",
                required: true,
                input_html: { autocomplete: "current-password" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Update" %>
  </div>
<% end %>

<h3>Cancel my account</h3>

<div>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?" }, method: :delete %></div>

<%= link_to "Back", :back %>

<h2>Sign up</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= csrf_meta_tags %>
  <%= form_authenticity_token %>
  <%= f.error_notification %>
  <div class="form-inputs">
    <%= f.input :email,
                required: true,
                autofocus: true,
                input_html: { autocomplete: "email" }%>
    <%= f.input :password,
                required: true,
                hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
                input_html: { autocomplete: "new-password" } %>
    <%= f.input :password_confirmation,
                required: true,
                input_html: { autocomplete: "new-password" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

<h2>Log in</h2>

<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
  <%= csrf_meta_tags %>
  <%= form_authenticity_token %>
  <div class="form-inputs">
    <%= f.input :email,
                required: false,
                autofocus: true,
                input_html: { autocomplete: "email" } %>
    <%= f.input :password,
                required: false,
                input_html: { autocomplete: "current-password" } %>
    <%= f.input :remember_me, as: :boolean if devise_mapping.rememberable? %>
  </div>

  <div class="form-actions">
    <%= form_authenticity_token %>
    <%= f.button :submit, "Log in" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>
<h2>Resend unlock instructions</h2>

<%= simple_form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %>
  <%= csrf_meta_tags %>
  <%= form_authenticity_token %>
  <%= f.error_notification %>
  <%= f.full_error :unlock_token %>

  <div class="form-inputs">
    <%= f.input :email,
                required: true,
                autofocus: true,
                input_html: { autocomplete: "email" } %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Resend unlock instructions" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

This is my application layout file:

<!DOCTYPE html>
<html>
  <head>
    <title>RailsRuby3</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
  </head>
      
  <body>
    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>
    <%= yield %>
  </body>
</html>

This is my development.rb file:

require "active_support/core_ext/integer/time"

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded any time
  # it changes. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable server timing
  config.server_timing = true

  # Enable/disable caching. By default caching is disabled.
  # Run rails dev:cache to toggle caching.
  if Rails.root.join("tmp/caching-dev.txt").exist?
    config.action_controller.perform_caching = true
    config.action_controller.enable_fragment_cache_logging = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      "Cache-Control" => "public, max-age=#{2.days.to_i}"
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  config.action_mailer.default_url_options = { host: 'Wishlist-Website.dabbous1.repl.co', port: 3000 }

  # Store uploaded files on the local file system (see config/storage.yml for options).
  config.active_storage.service = :local

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise exceptions for disallowed deprecations.
  config.active_support.disallowed_deprecation = :raise

  # Tell Active Support which deprecation messages to disallow.
  config.active_support.disallowed_deprecation_warnings = []

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Highlight code that triggered database queries in logs.
  config.active_record.verbose_query_logs = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations.
  # config.i18n.raise_on_missing_translations = true

  # Annotate rendered view with file names.
  # config.action_view.annotate_rendered_view_with_filenames = true

  # Uncomment if you wish to allow Action Cable access from any origin.
  # config.action_cable.disable_request_forgery_protection = true

  # Allow hosting on *.repl.co hosts
  config.hosts << /.*\.repl.co/
end

This my user.rb file

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
end

And this is my routes file:

Rails.application.routes.draw do
  devise_for :users
  resources :wishlists
  root to: 'wishlists#home'
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
end
Aly Dabbous
  • 107
  • 1
  • 7
  • The CRSF meta tags are only used when sending XHR requests without a form. Otherwise the hidden inputs in the form are used. – max Mar 26 '23 at 10:22
  • What is this `<%= form_authenticity_token %>` method? At best its unnessicary but could actually be the issue. You don't actually need to manually add the authenticity token. Thats done by `form_for` which Simple Form wraps. – max Mar 26 '23 at 10:31
  • Actually it worked at the end when I added <%= form_authenticity_token %> in all my forms – Aly Dabbous Mar 26 '23 at 13:43
  • Happy that it worked out but I don't think stacking more garbage onto your code is a good long term solution. You should probally get rid of whatever this hack is and figure out why your code doesn't work without it. This isn't normal. – max Mar 27 '23 at 12:15

0 Answers0