我是rails的新手,并尝试实现用户注册页面(在CRUD中创建)。除了提交按钮,我还想要一个取消按钮,以便用户可以单击它并返回索引页面。我已完成以下操作,但取消按钮仍然注册用户。
Haml代码(new.html.haml
):
=form_tag users_path, :method => :post do
%p= label :user, :username, "User Name"
%p= text_field :user, :username
... (other information)
= button_tag "Submit", :type => 'submit'
= button_tag "Cancel", :type => 'cancel'
Rails代码(users_controller.rb
):
class UsersController < ApplicationController
before_filter :check_for_cancel, :only => [:create, :update]
def create
vals = params[:user]
if(User.exists(vals[:username]))
flash[:warning] = "#{vals[:username]} already exists! Please try a new one. "
else
vals[:create_date] = DateTime.current
vals.except!(:confirm_password)
@user = User.create(vals, :without_protection => :true)
unless @user==nil
flash[:notice] = "#{vals[:username]} has been registered. "
else
flash[:warning] = "#{vals[:username]} has not been registered successfully. "
end
end
redirect_to users_path
end
...(other CRUD methods)
def check_for_cancel
if(params[:commit]=="cancel")
flash[:notice] = "Registration is cancelled. "
redirect_to users_path
end
end
end
感谢。
答案 0 :(得分:1)
我不太明白它是如何工作的,但经过一些实验后,以下代码可以正常工作。 在haml中,
= button_tag "Submit", :type => 'submit', :name => 'submit'
= button_tag "Cancel", :type => 'cancel', :name => 'cancel'
在rails中,
def check_for_cancel
if(params.key?("cancel"))
flash[:notice] = "Registration is cancelled. "
redirect_to users_path
end
end
基本上,当我为按钮指定名称时,params
会有一个条目params["cancel"]=""
或params["submit"]=""
,具体取决于所点击的按键。
答案 1 :(得分:0)
唯一合法的按钮类型是submit
,reset
和button
(http://developer.mozilla.org/en-US/docs/Web/HTML/Element/button)。 cancel
不是type
属性的有效值。
您必须创建一个变通方法,以便在按钮提交中传递“取消”消息。有关选项,请参阅上一个 How to create a HTML Cancel button that redirects to a URL
答案 2 :(得分:0)
您可以使用link_to并将其设置为带有css的按钮,然后您无需在控制器中进行任何检查。
= link_to 'Cancel', users_path, class: 'btn'
答案 3 :(得分:0)
如果您所在的开发团队拥有专门的设计人员,可以创建前端标记,然后无缝轻松地工作,我建议您使用html和erb而不是haml来渲染您的视图。 使用html和erb实现所需内容的简单形式是
<%= form_for(@user) do |f| %>
<% 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 %>
<div class="field">
<%= f.label :user_name %><br />
<%= f.text_field :user_name %>
</div>
<div class="field">
<%= f.label :first_name %><br />
<%= f.text_field :first_name %>
</div>
<div class="field">
<%= f.label :last_name %><br />
<%= f.text_field :last_name %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.number_field :phone %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
但是如果你仍然坚持使用haml原因,那么
= form_for(@user) do |f|
- if @user.errors.any?
#error_explanation
%h2
= pluralize(@user.errors.count, "error")
prohibited this user from being saved:
%ul
- @user.errors.full_messages.each do |msg|
%li= msg
.field
= f.label :user_name
%br/
= f.text_field :user_name
.field
= f.label :first_name
%br/
= f.text_field :first_name
.field
= f.label :last_name
%br/
= f.text_field :last_name
.field
= f.label :phone
%br/
= f.number_field :phone
.actions
= f.submit
\#{link_to 'Back', users_path}
如上所示,在这种情况下,“link_to”辅助方法将导航到指定的路径 users_path。查找有关link_to帮助方法http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to
的更多信息答案 4 :(得分:-1)
<%= button_tag "Cancel", type: :reset %>
这项工作对我来说
更多参考: - http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html