2010-06-10 85 views
5

我怎样才能跳到控制器中的另一个动作?如何直接提交按钮到另一个动作

我有表单和几个提交按钮。每个子提交按钮都有名称。

<g:form action="save" method="post"> 
    <g:input name="title" value="${letter.title}" /> 
    <g:input name="comments[0].text" value="${letter.comments[0].text}" /> 
    <g:submitButton name="save" value="save" /> 
    <g:submitButton name="addComment" value="add" /> 
</g:form> 

def save = { 

    if (params.addComment){ 
     letter.addToComents( new Comment()) 
     render(view:'form', model:["letter": letter]) 
     return 
    } 

    ... 
    if (letter.save()) 
    ... 
} 

def addComment = { 
     ... 
    } 

它有效,但它不好。我想从块“addComment”移动代码到动作add评论:

def save = { 

    if (params.addComment){ 
     // it don´t work 
     redirect (action:"addComment") 
    } 

    ... 
    if (letter.save()) 
    ... 
} 

def addComment = { 
     letter.addToComents( new Comment()) 
     render(view:'form', model:["letter": letter]) 
     return 
    } 

或者它存在更好的解决方案吗? 这将是很好:

<g:submitButton name="save" value="save" action="save" /> 
<g:submitButton name="addComment" value="add" action="addComment" /> 

非常感谢 汤姆

回答

13

使用g:actionSubmit标签来代替。

 <g:form method="post"> 
      <g:input name="title" value="${letter.title}" /> 
      <g:input name="comments[0].text" value="${letter.comments[0].text}" /> 
      <g:actionSubmit action="save" value="Save" /> 
      <g:actionSubmit action="addComment" value="Add Comment" /> 
     </g:form> 
+4

谢谢,我很笨。 – 2010-06-10 14:53:50

+1

@tom LOL一个幸运的愚蠢(看到你的个人资料8后) – 2012-09-08 23:21:14

0

对于那些谁使用Twitter的引导插件(或需要除了在按钮上的文字的东西),并希望将glyphicon添加到该按钮,您将需要使用的按钮标签。所以,你需要做的是这样

代码片段1.

<g:form role="form" method="post"> 
     ...your inputs 

    <button type="submit" name="_action_save"> 
    <span class="glyphicon glyphicon-ok"></span> 
    Save 
    </button> 

    <button type="submit" name="_action_saveAndNew"> 
     <span class="glyphicon glyphicon-ok"></span> 
     Save and New 
    </button> 
    </g:form> 

凡在你的按钮,您将需要使用前缀

_action_ 

指定动作的名称来得到这样的事情

name="_action_yourActionName" 

只是有点提醒,因为我使用twitter Bottstrap插件3.0这是你怎么加glyphicon

<span class="glyphicon glyphicon-ok"></span> 

代码片段1也有类似的行为:

<g:form role="form" method="post"> 
     ...your inputs   

    <g:actionSubmit action="save" value="Save" /> 

    <g:actionSubmit action="saveAndNew" value="Save and New" /> 

    </g:form> 

到底这个例子可以帮助您有一个情况类似的行为actionSubmit可以,你不想要或不能使用它。这只是一种选择,只要有可能,最好使用actionSubmit。

相关问题