之前做课设的时候用 ROR Tutorial 学了一段时间的 Rails。现在想自己深入学一下 Rails 这块,就重新捡起来。而且也想试试 Rails 7 与教程中不兼容的地方,并且解决这些问题来提升自己。 (不过这次我自己搞不定了)
wsl 1
Gemfile:
# source "https://rubygems.org"
source "https://gems.ruby-china.com"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "3.0.2"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.2", ">= 7.0.2.2"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
# Use Redis adapter to run Action Cable in production
gem "redis", "~> 4.0"
# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
# Use Sass to process CSS
# gem "sassc-rails"
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
gem "bootstrap-sass"
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
gem "byebug"
end
group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
gem "guard"
gem "guard-minitest"
end
group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem "webdrivers"
gem "minitest-reporters"
gem "rails-controller-testing"
end
gem "erb-formatter", "~> 0.3.0"
在ROR Tutorial 7.3 注册失败 - (需要科学上网),中的 7.3.3 错误消息提示小节。
错误消息未提示。
经查找,发现是 render 之后的@user为 nil 导致。但是依照代码来说,@user应该留存的是 controller 的值。难道说现在的 Rails 在 render 之后会刷新 erb 中引用 controller 的值吗?
_errors_messages.html.erb
<% if @user.errors.any? %>
<div id="error_explanation">
<div class = "alert alert-danger">
The form contains <%= pluralize(@user.errors.count, "error") %>
</div>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</div>
<% end %>
users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
end
def show
@user = User.find(params[:id])
end
def create
@user = User.new(user_params)
if @user.save
else
render 'new'
# debugger
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
new.html.erb
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :email %>
<%= f.email_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
具体代码项目在这 (Github)