铁轨强参数不允许传递数据

发布于 2025-01-22 04:35:59 字数 2249 浏览 3 评论 0 原文

我正在尝试在FE和Rails BE上创建一个身份验证组件。

当我向API发送请求以创建用户时,我不确定这里有什么问题。不允许密码,应用程序中断。

我对FE的注册操作:

export const signupUser = createAsyncThunk('user/signupUser', 
    async(userData, thunkAPI) => {
        console.log('userData',userData)
        let url = 'http://localhost:3000/users';
        let apiObject = {
            method:'POST',
            headers: {
                accept: "application/json",
                "content-type":"application/json"
            },
            body: JSON.stringify({
                user_name: userData.name,
                email: userData.email,
                password: userData.password,
                password_confirmation: userData.password_confirmation
            })
        }
        fetch(url, apiObject)
        .then(response => response.json())
        .then(data => console.log(data))
    }

)

用户模型

class User < ApplicationRecord
    has_secure_password

    validates :user_name, presence: true
    validates :email, uniqueness: true, presence: true

    # has_many :reports dependent: :destroy
    # has_many :properties dependent: :destroy
end

BE: BE上的用户/控制器上的

class UsersController < ApplicationController


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

    def create
        user = User.new(user_params)
         binding.pry
        if user.save && user.authenticate(user_params[:password])
            binding.pry
            token = encode_token({user_id: user.id})
            render json: { user: UserSerializer.new(user).serializable_hash, token: token}, status: :created
        else
            render json: {error: user.errors.full_messages.to_sentence}, status: :unprocessable_entity
        end

    end

    private 

    def user_params
        params.require(:user).permit(:user_name, :email, :password, :password_confirmation)
    end
end

:我使用pry尝试理解,所以这是流程: UI-注册 在此处输入图像描述

,这是当params无法传递密码时的断点。 在此处输入图像描述

任何人可以帮忙吗?

I am trying to create an authentication component on with React on the FE and Rails BE.

I'm not sure what's the issue here, when i send a request to the API to create the user. the password is not permitted and the application breaks.

My Signup action on the FE:

export const signupUser = createAsyncThunk('user/signupUser', 
    async(userData, thunkAPI) => {
        console.log('userData',userData)
        let url = 'http://localhost:3000/users';
        let apiObject = {
            method:'POST',
            headers: {
                accept: "application/json",
                "content-type":"application/json"
            },
            body: JSON.stringify({
                user_name: userData.name,
                email: userData.email,
                password: userData.password,
                password_confirmation: userData.password_confirmation
            })
        }
        fetch(url, apiObject)
        .then(response => response.json())
        .then(data => console.log(data))
    }

)

My User Model on the BE:

class User < ApplicationRecord
    has_secure_password

    validates :user_name, presence: true
    validates :email, uniqueness: true, presence: true

    # has_many :reports dependent: :destroy
    # has_many :properties dependent: :destroy
end

user/controller on the BE:

class UsersController < ApplicationController


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

    def create
        user = User.new(user_params)
         binding.pry
        if user.save && user.authenticate(user_params[:password])
            binding.pry
            token = encode_token({user_id: user.id})
            render json: { user: UserSerializer.new(user).serializable_hash, token: token}, status: :created
        else
            render json: {error: user.errors.full_messages.to_sentence}, status: :unprocessable_entity
        end

    end

    private 

    def user_params
        params.require(:user).permit(:user_name, :email, :password, :password_confirmation)
    end
end

I used pry to try to understand so here's the flow:
UI- signup
enter image description here

and this is the breakpoint when the params can't pass the passwords.
enter image description here

Can anyone help with this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好倦 2025-01-29 04:35:59

控制器期望以下结构中的有效载荷为
定义为: params.require(:user).permit(:user_name,:emer_name,:email,:password,:passwand_confirnration)

{
   user: 
     {
        username: <value>,
        email: <value>,
        password: <value>,
        password_confirmation: <value>
     }
}

在您的注册操作中,相应地传递有效载荷:

export const signupUser = createAsyncThunk('user/signupUser', 
    async(userData, thunkAPI) => {
        console.log('userData',userData)
        let url = 'http://localhost:3000/users';
        let apiObject = {
            method:'POST',
            headers: {
                accept: "application/json",
                "content-type":"application/json"
            },
            body: JSON.stringify({
                user: {
                    user_name: userData.name,
                    email: userData.email,
                    password: userData.password,
                    password_confirmation: userData.password_confirmation
                }
            })
        }
        fetch(url, apiObject)
        .then(response => response.json())
        .then(data => console.log(data))
    }
)

有关进一步阅读,请参阅在这里。

The controller expects the payload in the following structure as
defined by: params.require(:user).permit(:user_name, :email, :password, :password_confirmation)

{
   user: 
     {
        username: <value>,
        email: <value>,
        password: <value>,
        password_confirmation: <value>
     }
}

In your SignUp action, pass the payload accordingly:

export const signupUser = createAsyncThunk('user/signupUser', 
    async(userData, thunkAPI) => {
        console.log('userData',userData)
        let url = 'http://localhost:3000/users';
        let apiObject = {
            method:'POST',
            headers: {
                accept: "application/json",
                "content-type":"application/json"
            },
            body: JSON.stringify({
                user: {
                    user_name: userData.name,
                    email: userData.email,
                    password: userData.password,
                    password_confirmation: userData.password_confirmation
                }
            })
        }
        fetch(url, apiObject)
        .then(response => response.json())
        .then(data => console.log(data))
    }
)

For further reading, refer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文