表单中的Button_to标签关闭窗体 [英] Button_to tag in a form closes form

查看:193
本文介绍了表单中的Button_to标签关闭窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Ruby on Rails 3中使用表单。所以在我的项目表单中,我有一个按钮可以将用户从这个项目中签出。然而,奇怪的是, button_to 标签关闭了我的整个表单,我无法再使用我的提交按钮。



所以我尝试了很多,但我不明白为什么会发生这种情况。



所以这是我的表单:

 <%= form_for @project,html:{class:form-horizo​​ntal project} do | f | %GT; 
< div>
<%= f.label:users,'Project Users:',:class => 'control-label',style:'margin-bottom:20px'%>
< div>
<%@project.users.order('last_name')。each | do | user | %GT;
< div style =margin-bottom:15px>
<%= user.name%>
<%= button_to'注销',sign_user_out_project_path(user_id:user.id,id:@project),方法:: delete,class:'btn btn-danger btn-xs pull-right',style: 'margin-right:600px; margin-top:-20px'%>
< / div>
<%end%>
< / div>
< / div>


<%= f.submit nil,:class => 'btn btn-primary'%>
<%= link_to t('。cancel',:default => t(helpers.links.cancel)),
projects_path,:class => 'btn btn-default'%>

<%end%>

在我看来,这段代码看起来很好,因为所有的标签都完全封闭。不过,我认为使用 button_to 时可能会有一些魔力,所以也许有人知道更好的方式来做我想做的事情。



谢谢!

解决方案

不要把 button_to 在您的表单中。



根据 docs


button_to

-

创建一个包含一个按钮的表单,该按钮提交给由该组选项创建的URL。 p>

HTML表单有一个清晰规范,你应该坚持,只要你实现一个。

在表单中包含表单会导致外部表单停止工作(HTML无法处理另一个< form> < / form>






简单的答案是从表单中删除 button_to ,并将其放在外面,(在您的情况下),用另一个元素替换它( link_to )。如果您希望链接看起来像一个按钮,您可以使用 <按钮> 标记创建按钮:

 <%= form_for @project,html:{class:form-horizo​​ntal project} do | f |%> 
< div>
<%= f.label:users ,'Project Users:',:class =>'control-label',style:'margin-bottom:20px'%>
< div>
<%@ project.users。 order('last_name')。each do | user |%>
< div style =margin-bottom :15px>
<%= user.name%>
<%= link_to'< button>注销< /按钮>'。html_safe,sign_user_out_project_path(user_id:user.id,id:@project),方法:: delete,class:'btn btn-danger btn-xs pull-right',style:'margin-right:600px; margin-top:-20px'%>
< / div>
<%end%>
< / div>
< / div>

<%= f.submit nil,:class => 'btn btn-primary'%>
<%= link_to t('。cancel',:default => t(helpers.links.cancel)),
projects_path,:class => 'btn btn-default'%>

<%end%>


I am currently working with form in ruby on rails 3. So in my project form I have a button to sign users out of this project. However, the strange thing is, that the button_to tag closes my whole form and i cannot use my submit button anymore.

So I tried a lot, but i do not understand why this happens.

So this is my form:

<%= form_for @project, html: { class: "form-horizontal project" } do |f| %>
    <div>
          <%= f.label :users, 'Project Users:',  :class => 'control-label', style: 'margin-bottom: 20px' %>
          <div>
            <% @project.users.order('last_name').each do |user| %>
                      <div style="margin-bottom: 15px">
                        <%= user.name %>
                          <%= button_to 'Sign Out', sign_user_out_project_path(user_id: user.id, id: @project), method: :delete, class: 'btn btn-danger btn-xs pull-right', style:'margin-right: 600px; margin-top: -20px' %>
                      </div>
            <% end %>
          </div>
        </div>


      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                projects_path, :class => 'btn btn-default' %>

    <% end %>

In my opinion this code looks fine, as all tags are perfectly closed. However, i think there might be some magic when using button_to so maybe someone knows a better way to do what I want to do.

Thanks!

解决方案

Don't put a button_to inside your form.

As per the docs:

(button_to) Generates a form containing a single button that submits to the URL created by the set of options

--

HTML forms have a clear spec, and you should adhere to that whenever you implement one.

Including a form within a form causes the "outer" form to stop working (HTML can't process another <form> before </form>.


The simple answer is to remove your button_to from within your form & put it outside, or (in your case), replace it with another element (link_to). If you want the link to look like a button, you can use <button> markup to create a button:

<%= form_for @project, html: { class: "form-horizontal project" } do |f| %>
    <div>
          <%= f.label :users, 'Project Users:',  :class => 'control-label', style: 'margin-bottom: 20px' %>
          <div>
            <% @project.users.order('last_name').each do |user| %>
                      <div style="margin-bottom: 15px">
                        <%= user.name %>   
                        <%= link_to '<button>Sign Out</button>'.html_safe, sign_user_out_project_path(user_id: user.id, id: @project), method: :delete, class: 'btn btn-danger btn-xs pull-right', style:'margin-right: 600px; margin-top: -20px' %>
                      </div>
            <% end %>
          </div>
        </div>

      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                projects_path, :class => 'btn btn-default' %>

<% end %>

这篇关于表单中的Button_to标签关闭窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆