【问题标题】:Rails form_tag cancel button doesn't workRails form_tag取消按钮不起作用
【发布时间】:2014-07-20 15:45:48
【问题描述】:

我是 Rails 新手,正在尝试实现用户注册页面(在 CRUD 中创建)。除了提交按钮之外,我还想要一个取消按钮,以便用户可以单击它并返回索引页面。我已完成以下操作,但取消按钮仍会注册用户。

哈姆代码(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

谢谢。

【问题讨论】:

  • 在您的check_for_cancel 方法中,尝试使用redirect_to users_url。本质上,尝试使用_url 而不是_path 后缀。
  • @theSshow 它不起作用。实际上,当我故意使网站崩溃并检查参数的调试信息时,我什至看不到 :commit 作为键。那部分正确吗?
  • 实际上,合法的按钮类型只有submitresetbuttondeveloper.mozilla.org/en-US/docs/Web/HTML/Element/button)。 cancel 不是 type 属性的有效值。也许您最好只创建一个链接并设置一个按钮的样式,如下所示:stackoverflow.com/questions/18407832/…

标签: ruby-on-rails ruby haml crud


【解决方案1】:

我不太明白它是如何工作的,但经过一些实验后,以下代码可以工作。 在哈姆里,

= button_tag "Submit", :type => 'submit', :name => 'submit'
= button_tag "Cancel", :type => 'cancel', :name => 'cancel'

在轨道中,

def check_for_cancel
    if(params.key?("cancel"))
        flash[:notice] = "Registration is cancelled. "
        redirect_to users_path
    end
end

基本上,当我给按钮命名时,params 将有一个条目params["cancel"]=""params["submit"]="",具体取决于所单击的键。

【讨论】:

    【解决方案2】:

    唯一合法的按钮类型是submitresetbutton (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

    【讨论】:

      【解决方案3】:

      您可以使用 link_to 并将其设置为带有 css 的按钮,然后您不必在控制器中进行任何检查。

      = link_to 'Cancel', users_path, class: 'btn'
      

      【讨论】:

        【解决方案4】:

        如果您所在的开发团队拥有专门的设计师来创建前端标记,然后可以轻松无缝地工作,我建议您使用 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”帮助方法在这种情况下会导航到指定的路径 用户路径。查找有关 link_to 辅助方法http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to的更多信息

        【讨论】:

          【解决方案5】:

          &lt;%= button_tag "Cancel", type: :reset %&gt;这对我有用

          更多参考:-http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-02-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-14
            相关资源
            最近更新 更多