为什么我的创建表单不起作用,甚至没有响应?
Why isn't my Create Form working, or even responding?
所以我用的是form_with,我的代码放在views/users/new.html.erb
<% content_for :title, "Sign Up" %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:
</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_with model: @user do |f| %>
<%= f.label :email %>
<%= f.email_field :email, :placeholder => 'E-mail address' %><br>
<%= f.label :password %>
<%= f.password_field :password, :placeholder => 'Password' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, :placeholder => 'Password confirmation' %><br>
<label>Are you a cleaner or customer?</label>
<%= f.select :user, User::SUB_CLASSES, include_blank: true %><br>
<%= f.submit "Next" %>
<% end %>
我的控制器有以下代码
def new
@user = User.new
render :layout => "beforelogin"
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
if @user.sub_class == "Cleaner"
redirect_to new_cleaner_path
else
redirect_to new_customer_path
end
else
render :new
flash[:alert] = "Your registration could not be completed"
end
end
现在,当我在 user/new.html.erb 路线上填写表格时,没有任何反应。按钮只需点击一下。没有错误弹出。我什至尝试在表格顶部添加 <%= form_with model: @user, url: users_path, method: "post" do |f| %>
。还是一样的反应。
默认情况下 form_with
将 data-remote
属性附加到表单。因此,将使用 Ajax.
提交表单
如果您想禁用此功能,您需要将选项 local
设置为 true。
像这样:
<%= form_with model: @user, local: true do |f| %>
所以我用的是form_with,我的代码放在views/users/new.html.erb
<% content_for :title, "Sign Up" %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@user.errors.count, "error") %>
prohibited this user from being saved:
</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_with model: @user do |f| %>
<%= f.label :email %>
<%= f.email_field :email, :placeholder => 'E-mail address' %><br>
<%= f.label :password %>
<%= f.password_field :password, :placeholder => 'Password' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, :placeholder => 'Password confirmation' %><br>
<label>Are you a cleaner or customer?</label>
<%= f.select :user, User::SUB_CLASSES, include_blank: true %><br>
<%= f.submit "Next" %>
<% end %>
我的控制器有以下代码
def new
@user = User.new
render :layout => "beforelogin"
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
if @user.sub_class == "Cleaner"
redirect_to new_cleaner_path
else
redirect_to new_customer_path
end
else
render :new
flash[:alert] = "Your registration could not be completed"
end
end
现在,当我在 user/new.html.erb 路线上填写表格时,没有任何反应。按钮只需点击一下。没有错误弹出。我什至尝试在表格顶部添加 <%= form_with model: @user, url: users_path, method: "post" do |f| %>
。还是一样的反应。
默认情况下 form_with
将 data-remote
属性附加到表单。因此,将使用 Ajax.
如果您想禁用此功能,您需要将选项 local
设置为 true。
像这样:
<%= form_with model: @user, local: true do |f| %>