我是 Ruby on Rails 的初學者,剛開始通過 youtube 視頻學習。我正在嘗試創建一個注冊頁面。
路線.rb:
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
# get "/about" request, pass to about controller and perform index action(function)
get "/about", to: "about#index", as: :about
get "/sign_up", to: "registration#new"
post "/sign_up", to: "registration#create"
root "main#index"
end
Sign_up.html.erb
<h1 class="mb-5">Sign Up</h1>
<%= form_with model: @user, url: sign_up_path do |form| %>
<div class="mb-3">
<%= form.label :email %>
<%= form.text_field :email, class:"form-control", placeholder: "[email protected]" %>
</div>
<div class="mb-3">
<%= form.label :password %>
<%= form.password_field :password, class:"form-control", placeholder: "password" %>
</div>
<div class="mb-3">
<%= form.label :password_confirmation %>
<%= form.password_field :password_confirmation, class:"form-control", placeholder: "password" %>
</div>
<div class="mb-3">
<%= form.submit class: "btn btn-primary" %>
</div>
<% end %>
控制器.rb
class RegistrationController < ApplicationController
def new
# Since User model is created, this will look into database and create new instance on it
# Instance variable is used because it can be accessed in the view folder
@user = User.new
end
def create
# render plain: "Thanks"
render :create
end
end
創建.html.erb
<h1>Thanks!</h1>
控制臺/終端:
Started POST "/sign_up" for ::1 at 2022-06-19 02:54:23 0800
Processing by RegistrationController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}
Rendering layout layouts/application.html.erb
Rendering registration/create.html.erb within layouts/application
Rendered registration/create.html.erb within layouts/application (Duration: 0.2ms | Allocations: 15)
Rendered shared/_navbar.html.erb (Duration: 0.2ms | Allocations: 125)
Rendered shared/_flash.html.erb (Duration: 0.1ms | Allocations: 28)
Rendered layout layouts/application.html.erb (Duration: 4.5ms | Allocations: 2465)
Completed 200 OK in 6ms (Views: 5.3ms | ActiveRecord: 0.0ms | Allocations: 2921)
一切都很好,直到我按下表單的提交按鈕,它應該有發布請求,并且操作應該由 create 處理,該操作將呈現到 create.html,從控制臺,它狀態似乎 create.html 已呈現但瀏覽器仍然留在注冊頁面。我希望它在提交表單后呈現為 ??create.html。順便說一句,我什至無法在我剛剛注釋掉的 create 方法中呈現純文本。任何想法?謝謝。
Stefanyuk Yuriy 的回答更新: 首先,感謝您的回復,但是,在我提交表單后,我只使用導航欄呈現了一個空白頁面,“謝謝!” 沒有出現在瀏覽器中。
提交表單后的控制臺/終端:
Started POST "/sign_up" for ::1 at 2022-06-19 10:15:24 0800
Processing by RegistrationController#create as TURBO_STREAM
Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create User"}
Rendering registration/create.html.erb
Rendered registration/_thank.html.erb (Duration: 0.5ms | Allocations: 108)
Rendered registration/create.html.erb (Duration: 1.8ms | Allocations: 379)
Completed 200 OK in 4ms (Views: 2.4ms | ActiveRecord: 0.0ms | Allocations: 918)
目前我不確定問題出在哪里,有什么想法嗎?
uj5u.com熱心網友回復:
首先將您的表單移動到部分app/views/registration/_form.html.erb. 也移動Thank you snippet到部分并添加create.turbo_stream.erb內容如下所示的檔案。將您的表單包裝成turbo_frame_tag. 并更改您的控制器,如下所示:
# app/views/registration/_form.html.erb
<h1 class="mb-5">Sign Up</h1>
<%= form_with model: user, url: sign_up_path do |form| %>
<div >
<%= form.label :email %>
<%= form.text_field :email, class:"form-control", placeholder: "[email protected]" %>
</div>
<div class="mb-3">
<%= form.label :password %>
<%= form.password_field :password, class:"form-control", placeholder: "password" %>
</div>
<div >
<%= form.label :password_confirmation %>
<%= form.password_field :password_confirmation, class:"form-control", placeholder: "password" %>
</div>
<div class="mb-3">
<%= form.submit class: "btn btn-primary" %>
</div>
<% end %>
# app/views/registration/_thank.html.erb
<h1>Thanks!</h1>
# app/views/registration/new.html.erb
<%= turbo_frame_tag :sign_up_form do %>
<%= render 'form', user: @user %>
<% end %>
# app/views/registration/create.turbo_stream.erb
<% if @user.errors.any? %>
<%= turbo_stream.replace(:sign_up_form, partial: 'registration/form', locals: { user: @user }) %>
<% else %>
<%= turbo_stream.replace(:sign_up_form, partial: 'registration/thank_you') %>
<% end %>
class RegistrationController < ApplicationController
def new
@user = User.new
end
def create
@user = User.create(user_params)
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
注意:如果您還沒有save邏輯,只需注釋掉@user = User.create(user_params)并將所有代碼替換app/views/registration/create.turbo_stream.erb為
<%= turbo_stream.replace(:sign_up_form, partial: 'registration/thank_you') %>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/493382.html
