我想实现的功能是: 一个新用户提交注册表单之后,页面跳转到“类型选择”页。
问题出现在 select_type.html.erb 这个文件的表单中
<%= form_for( @user,url: {action: "update_type"} , html: {method: "patch"}) do |f| %>
<%= radio_button_tag(:type, 'teacher')%>
<%= label_tag(:type_teacher, '幼师')%>
<%= radio_button_tag(:type, 'school') %>
<%= label_tag(:type_school, '学校')%>
<%= radio_button_tag(:type, 'parent') %>
<%= label_tag(:type_parent, '家长')%>
<%= f.submit "提交选择", class: "btn btn-large btn-primary"%>
<% end %>
UsersController 中的代码如下:
class UsersController < ApplicationController
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the Sample App!"
render 'select_type'
else
render 'new'
end
end
def new
@user = User.new
end
def index
end
def show
end
def update
end
def select_type
@user= User.find(params[:id])
end
def update_type
@user = User.find(params[:id])
type = params[:type]
if type == 'school'
data = 1
end
if type == 'parent'
data = 2
end
if type == 'teacher'
data = 3
end
if @user.update(user_type: data)
flash[:success] = "您成功开通了服务!"
redirect_to @user
else
render 'select_type'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
routes.rb 文件的代码如下:
resources :users do
member do
get 'select_type'
patch 'update_type'
end
end
执行 rake routes 之后显示的路由为:
select_type_user GET /users/:id/select_type(.:format) users#select_type
update_type_user PATCH /users/:id/update_type(.:format) users#update_type
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
当我在地址栏中输入: localhost:3000/users/1/select_type,页面可以成功访问 select_type.html 页面
但当我,先注册用户,提交注册表单之后,就报错 No route matches {:action=>"update_type", :controller=>"users"}
我查过文档之后,发现,把 表单内容改为 <%= form_for( @user,url: update_type_user_path(@user) , html: {method: "patch"}) do |f| %> 就可以在提交注册表单之后,直接跳转到类型选择界面了
这两句到底有什么不同呢?
<%= form_for( @user,url: update_type_user_path(@user) , html: {method: "patch"}) do |f| %> <%= form_for( @user,url: {action: "update_type"} , html: {method: "patch"}) do |f| %>