dimanche 28 août 2022

Why is my ActionMailer throwing up Unpermitted parameter: :user?

So im trying to set up action mailer and I followed along with the official docs https://guides.rubyonrails.org/action_mailer_basics.html...Now when I try to sign up I get an error

Started POST "/signup" for 127.0.0.1 at 2022-08-28 15:12:10 +0530
Processing by UsersController#create as */*
  Parameters: {"name"=>"Yash", "email"=>"yashsawant932@gmail.com", "password"=>"[FILTERED]", "user"=>{"name"=>"Yash", "email"=>"yashsawant932@gmail.com"}}
Unpermitted parameter: :user
  TRANSACTION (0.1ms)  BEGIN
  ↳ app/controllers/users_controller.rb:5:in `create'
  User Exists? (0.8ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "yashsawant932@gmail.com"], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:5:in `create'
  TRANSACTION (5.4ms)  ROLLBACK
  ↳ app/controllers/users_controller.rb:5:in `create'
[active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.04ms)
Completed 422 Unprocessable Entity in 313ms (Views: 0.3ms | ActiveRecord: 6.3ms | Allocations: 5065)


Pls check out my code and let me know what im missing ?Also is my action mailer set up properly User controller

class UsersController < ApplicationController
  rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
  skip_before_action :authorize, only: [:create]
   def create
      user = User.create!(user_params)
      session[:user_id] = user.id
      render json: user, status: :created
      UserMailer.with(user: @user).welcome_email.deliver_later
    end
    
    def show
      render json: @current_user
    end

    def index
      user=User.all 
      render json: user
    end


  private     
  def user_params
      params.permit(:name,:email,:password)

  end
  def render_unprocessable_entity_response(invalid)
    render json: { errors: invalid.record.errors.full_messages }, status: :unprocessable_entity
  end
end

Application mailer

class ApplicationMailer < ActionMailer::Base
  default from: 'from@example.com'
  layout 'mailer'
end


Usermailer.rb

class UserMailer < ApplicationMailer
    default from: 'welcome@bigapple.com'

    def welcome_email
      @user = params[:user]
      @url  = 'http://example.com/login'
      mail(to: @user.email, subject: 'Welcome to Big Apple Explorer')
    end
end

views/user_mailer/welcome_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  </head>
  <body>
    <h1>Welcome to bigapple.com, <%= @user.name %></h1>
    <p>
      You have successfully signed up  & we are soo happy you are here.
      Now onwards you will get regular updates about the app.Feel free to 
      email back for any issues.
    </p>
   
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

text.erb

Welcome to bigapple.com, <%= @user.name %>

You have successfully signed up  & we are soo happy you are here.
Now onwards you will get regular updates about the app.Feel free to 
email back for any issues.


Thanks for joining and have a great day!

Also this is my app controller

class ApplicationController < ActionController::API
  include ActionController::Cookies
  rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
  before_action :authorize
  
   private
   def authorize
     puts "hello",session[:user_id]
     @current_user = User.find_by(id: session[:user_id])
      render json: { errors: ["Not authorized"] }, status: :unauthorized unless @current_user
   end
 
   def render_unprocessable_entity_response(exception)
     render json: { errors: exception.record.errors.full_messages }, status: :unprocessable_entity
   end
end

other than this i added nothing in my code becuz this was the online thing given no config settings nothing...

Aucun commentaire:

Enregistrer un commentaire