Ruby后置法

o3imoua4  于 2023-03-17  发布在  Ruby
关注(0)|答案(1)|浏览(88)

我是ruby的新手,我正在学习一个初学者教程,我创建了一个注册表单,但是提交按钮似乎没有正常工作。

注册控制器.rb:

class RegistrationController < ApplicationController
    def new
        @account=Account.new
    end

    def create
        @account=Account.new(params_account)
        if @account.save
           redirect_to root_path ,notice:"Successfully created account"
        else
            flash[:alert]="something went wrong"
            render :new
        end
    end

    def params_account
        params.require(:account).permit(:mail ,:passwor ,:password_confirmation)
    end
end

路由.rb:

Rails.application.routes.draw do
 
  get "about" ,to:"about#index"

  get "sign_up" , to:"registration#new"
  post "sign_up" , to: "registration#create"
  
  get "/", to: "main#index"
end

新的.html.erb文件:

<h1>Sign up</h1>
<%= form_with model: @account ,url:sign_up_path do |form| %>
<% if @account.errors.any? %>
 <div class="alert alert-danger">
    <% @account.errors.full_messages.each do |message|%>
       <div class="mb-4"><%= message %></div>
    <% end %>
</div>
<% end %>
<%= form.label :Email  %>
<div class="mb-4">
    
    <%= form.text_field :mail ,class:"form_control" ,placeholder:"rahma@gmail.com"%>
</div>
<%= form.label :Password  %>
<div class="mb-4">
    <%= form.password_field :password ,class:"form_control" ,placeholder:"password"%>
</div>
<%= form.label :Confirme_password  %>
<div class="mb-4">
    <%= form.password_field :password_confirmation ,class:"form_control" ,placeholder:"confirme your password"%>
</div>
<div class="mb-4">
    <%= form.submit "Sign up",class:"btn btn-primary"%>
</div>
<% end %>

我看了看终端后,我启动了服务器,它显示:

Started POST "/sign_up" for 127.0.0.1 at 2023-03-15 19:21:48 +0100
Processing by RegistrationController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "account"=>{"mail"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  Rendering text template
  Rendered text template (Duration: 0.0ms | Allocations: 4)
Completed 200 OK in 2ms (Views: 0.5ms | ActiveRecord: 0.0ms | Allocations: 492)

而且没有错误,当我试图提交一些数据时,即使它们的格式很好,它也引发了一个错误:

Started POST "/sign_up" for 127.0.0.1 at 2023-03-15 19:37:56 +0100
Processing by RegistrationController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "account"=>{"mail"=>"ahmed@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Unpermitted parameter: :password. Context: { controller: RegistrationController, action: create, request: #<ActionDispatch::Request:0x00007f62d4bca258>, params: {"authenticity_token"=>"[FILTERED]", "account"=>{"mail"=>"ahmed@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up", "controller"=>"registration", "action"=>"create"} }
  Rendering layout layouts/application.html.erb
  Rendering registration/new.html.erb within layouts/application
  Rendered registration/new.html.erb within layouts/application (Duration: 11.6ms | Allocations: 1708)
  Rendered shared/_flash.html.erb (Duration: 0.2ms | Allocations: 19)
  Rendered layout layouts/application.html.erb (Duration: 24.9ms | Allocations: 4019)
Completed 200 OK in 34ms (Views: 28.3ms | ActiveRecord: 0.0ms | Allocations: 5267)

我按照教程一步一步,一切为他工作,但不是我,我找不到问题在哪里.

gupuwyp2

gupuwyp21#

Processing by RegistrationController#create as TURBO_STREAM

您的表单请求通过Turbo处理
您可以通过添加data-turbo="false"属性来禁用此行为:

<%= form_with model: @account, url: sign_up_path, data: { turbo: false } do |form| %>

此外,您在强参数中存在排印错误(:password而非:passwor),正确版本为

def params_account
  params.require(:account).permit(:mail, :password, :password_confirmation)
end

相关问题