Am new to ruby on rails. Here am trying to post html form to ruby on rails (5.2.3 versions) To this effect, I reference solution found here link
<form accept-charset="UTF-8" method='post' action='http://localhost:3000/user1s'>
<label for='name'>Name</label>
<input type="text" name='name' placeholder='Enter your name'> <br>
<label for='password'>password</label>
<input type="text" name='password' placeholder='Enter your password'> <br>
<label for='email'>confirm pass</label>
<input type="text" name='password_confirmation' placeholder='Enter your password_confirmation'> <br>
<input type="submit" value='Save'>
</form>
user1 controller
class User1sController < ApplicationController
# POST /user1s
# POST /user1s.json
def create
@name = params[:name]
@password = params[:password]
@password_confirmation = params[:password_confirmation]
@user1 = User1.new(name=>@name, password=>@password , password_confirmation=>@password_confirmation)
respond_to do |format|
if @user1.save
format.html { redirect_to @user1, notice: 'User1 was successfully created.' }
format.json { render :show, status: :created, location: @user1 }
#format.json {status: :created }
else
format.html { render :new }
format.json { render json: @user1.errors, status: :unprocessable_entity }
end
end
end
end
Here is config/routes.rb
Rails.application.routes.draw do
#resources :user1s
post '/user1s', to: 'user1s#create', as: 'user1s'
end
When I run the code am getting the following error
The browser returned a 'null' origin for a request with origin-based forgery protection turned on. This usually means you have the 'no-referrer' Referrer-Policy header enabled, or that the request came from a site that refused to give its origin. This makes it impossible for Rails to verify the source of the requests. Likely the best solution is to change your referrer policy to something less strict like same-origin or strict-same-origin. If you cannot change the referrer policy, you can disable origin checking with the Rails.application.config.action_controller.forgery_protection_origin_check setting.
To mitigate the error above, I referenced link here which suggests solutions below yet cannot get it to work. link 2
at config/application.rb i have
Bundler.require(*Rails.groups)
module Saverecord
class Application < Rails::Application
Rails.application.config.action_controller.forgery_protection_origin_check = false
config.action_controller.allow_forgery_protection = false
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
end
end
at application controller I have
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
#protect_from_forgery with: :exception
protect_from_forgery with: :null_session
end